Write data to two columns with Python CSV writer

Posted December 8, 2020 290 views

I am scrapping a website data and want to write that data in two different columns but all data is printing in same single column

This is the code:

from bs4 import BeautifulSoup
from requests_html import HTMLSession
import csv

s = HTMLSession()
url = f''
list_data = []
r = s.get(url)
soup = BeautifulSoup(r.html.html, 'html.parser')
file = open('OutPut.csv', 'w')
writer = csv.writer(file)
writer.writerow(['Product Name', 'Specification'])

products ='#contentcenter_specs_externalnav_2 a')
specs ='#contentcenter_specs_internalnav_2 td')
for item in products:
    a = item.text
    print(a)    # want to write this in column 'Product Name'
    for i in specs:
        b = i.text
        print(b)   # want to write this in column 'Specification'

        writer.writerow([a, b])

How can I do that it will be great if you can help me with this

edited by MattIPv4
Submit an answer

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!