1

My code below, using csv.writer, is outputting to Excel, but only one row of data. When I print sheets I see all the data. How can I correct my write code so that all rows are exported to the .csv?

from bs4 import BeautifulSoup
import requests
import csv

headers = {'User-Agent': 'Mozilla/5.0'}

for i in range(1, 300):
    url = "***?page=%s" % i

    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.text, "html.parser")
    items = soup.find_all('div', class_='product-block__info')
    for item in items:
        product = item.find('span', class_='short_desc').text
        stock = item.find('span', class_='count_product_stock hidden').text
        brand = item.find('h4', class_='brand').text
        price = item.find('span', class_='selling_price').text

        # create a list of all the fields    
        sheets = [brand,product, stock, price]

        print(sheets)

        out = csv.writer(open("myfile.csv","w"), delimiter=',',quoting=csv.QUOTE_ALL)
        out.writerow(sheets)
Ben P
  • 3,267
  • 4
  • 26
  • 53

0 Answers0