Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

requests and BeautifulSoup returns weird string

Using BeautifulSoup and requests i have made a program that puts all the data of a few divs inside of one div with the class rightContent. Here is the code:

import requests
from bs4 import BeautifulSoup

url = "https://senf.ir/Company/5674580/%D8%A7%D9%87%D9%86-%D8%A7%D9%84%D8%A7%D8%AA"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
div = soup.find('div', {'class': 'rightContent'}) #<div class="rightContent">
print(div.text)

This code works very well, the only problem is the output look very weird and almost unusable.
Output:

 




اهن الات - آهن آلات و ضایعات






                                        استان :
                                    

گلستان





                                        شهر :
                                    

گرگان





                                        گروه :
                                    

صنعت





                                        زیر گروه :
                                    

آهن آلات و ضایعات





 آدرس  اهن الات:





گلستان-گرگان- بلوار استراباد- جنب نبروگاه فشار قوی برق - بازار اهن پلاک اول 





 مدیریت اهن الات:


مهرشاد قادری





                                تلفن :
                            

برای مشاهده اطلاعات لطفا
                                وارد شوید
                                    و در صورت عدم عضویت از اینجا 
                                ثبت نام  کنید
                                





                                همراه :
                            

برای مشاهده اطلاعات لطفا
                                وارد شوید
                                    و در صورت عدم عضویت از اینجا 
                                ثبت نام  کنید                              
                                





                                ایمیل :
                            







                                بازدید :
                            

2





Process finished with exit code 0

My Goal:
How could i turn this output into a dictionary?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

One of my main problems doing this was the randomly placed spaces and the language being persion not English.

>Solution :

Look for titles and values tags:

import requests
from bs4 import BeautifulSoup

url = "https://senf.ir/Company/5674580/%D8%A7%D9%87%D9%86-%D8%A7%D9%84%D8%A7%D8%AA"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.findAll('div', {'class': 'DetailsTitle'})
value = soup.findAll('div', {'class': 'DetailsValue'})

values = []
titles = []

for data in title: 
    values.append(data.get_text().replace('\n', '').replace('\r', '').replace(' ', ''))

for data in value: 
    titles.append(data.get_text().replace('\n', '').replace('\r', '').replace(' ', ''))

value_title_dict = dict(zip(values, titles))

value_title_dict would be:

{'استان :': 'گلستان', 'شهر :': 'گرگان', 'گروه :': 'صنعت', 'زیر گروه :': 'آهن آلات و ضایعات', ' آدرساهن الات:': 'گلستان-گرگان- بلوار استراباد- جنب نبروگاه فشار قوی برق - بازار اهن پلاک اول ', ' مدیریت اهن الات:': 'مهرشاد قادری', 'تلفن :': 'برای مشاهده اطلاعات لطفاوارد شویدو در صورت عدم عضویت از اینجا ثبت نامکنید', 'همراه :': 'برای مشاهده اطلاعات لطفاوارد شویدو در صورت عدم عضویت از اینجا ثبت نامکنید', 'ایمیل :': '', 'بازدید :': '40'}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading