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

Combine Two Different BeautifulSoup For Loops in a row

I’m getting data in two different for loops with BeautifulSoup, but I couldn’t find how to combine data from these two different loops in excel.

file = open('/path/test.csv', "a", encoding= "utf-8", newline='')
writer = csv.writer(file)
writer.writerow(["Phone Number","Firm Name"])
response = requests.get(url)
soup = BeautifulSoup(response.content, "lxml")

phones = soup.findAll("div", attrs={"class":"PhonesBox"})
names = soup.findAll("h2", attrs={"class":"CompanyName"})

try:  
        for phone in phones:
                firm_phone = phone.find("label").text
                writer.writerow([firm_phone])
        for name in names:
                firm_name = name.find("span").text      
                writer.writerow([firm_name])
except:
        pass

If I use it like this, of course I get the results one after the other, but what I need is ([firm_phone,firm_name]) in excel file. Any help appreciated.

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

>Solution :

If the Phone numbers and Firm Name does not need any further matching you can try assuming phones and names are lists:

file = open('/path/test.csv', "a", encoding= "utf-8", newline='')
writer = csv.writer(file)
writer.writerow(["Phone Number","Firm Name"])
response = requests.get(url)
soup = BeautifulSoup(response.content, "lxml")

phones = soup.findAll("div", attrs={"class":"PhonesBox"})
names = soup.findAll("h2", attrs={"class":"CompanyName"})

try:  
        for phone, name in zip(phones, names):
                firm_phone = phone.find("label").text
                firm_name = name.find("span").text 
                writer.writerow([firm_phone, firm_name])     
except:
        pass
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