i want to write listings to csv as coloumns. Every listing have more then 100 output. I give example of csv below thanks
csv output example:
coloumn 1 coloumn 2 coloumn 3 coloumn 4 coloumn 5
listing 1_output1 listing 2_output1 ... ... ...
listing 1_output2 listing 2_output2 ... ... ...
listing 1_output3 listing 2_output3 ... ... ...
... ... ... ... ...
import bs4 as bs
import urllib.request
import csv
sauce = urllib.request.urlopen('http...').read()
soup = bs.BeautifulSoup(sauce, 'lxml')
for listing in soup.find_all('div', class_='bigletter1'):
for listing2 in soup.find_all('td', class_='bigletter2'):
for listing3 in soup.find_all('td', class_='bigletter3'):
for listing4 in soup.find_all('td', class_='bigletter4'):
for listing5 in soup.find_all('td', class_='bigletter5'):
thank you for your answers but also i have non-numeric class also like class_='models' how can i write to in csv?
Use a range
to iterate over each listing. Append to a list
.
data = []
for i in range(1, 6):
data.append([])
for listing in soup.find_all('div', class_='bigletter{}'.format(i)):
data[-1].append(listing.text)
Now, data
looks somewhat like this -
[listing1,
listing2,
...
]
With each column as a sub-list. What we need is the transpose, to make it easy to write data to a CSV. This can be done with inverse zip
. Use the csv
module and write each row to a CSV file.
import csv
with open(file, 'w') as f:
w = csv.writer(f)
for row in zip(*data):
w.writerow(row)