Combine Two Different BeautifulSoup For Loops in a row

Question:

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.

Asked By: Do─čan Topuz

||

Answers:

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
Answered By: M H
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.