Question

Write data to two columns with Python CSV writer

Posted December 8, 2020 2k views
PythonDevelopment

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'https://everymac.com/systems/apple/iphone/index-iphone-specs.html'
list_data = []
r = s.get(url)
r.html.render(sleep=1)
soup = BeautifulSoup(r.html.html, 'html.parser')
file = open('OutPut.csv', 'w')
writer = csv.writer(file)
writer.writerow(['Product Name', 'Specification'])

products = soup.select('#contentcenter_specs_externalnav_2 a')
specs = soup.select('#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])
file.close()

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!