2017-09-21 34 views
2

我已經使用這裏描述的方法之一Python write to CSV line by line試圖將我的輸出的所有行寫入.CSV。我已經設法達到輸出和生成CSV的階段,但不是顯示我的數據的所有行,我看到一行,重複4次,沒有別的。Python CSV導出4個相同的行

任何人都可以看到這裏的問題是什麼?

from bs4 import BeautifulSoup 
import requests 
import csv 

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

for i in range(1, 300): 
    url = "xxx?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) 

     with open('csvfile.csv','wt') as file: 
      for l in sheets: 
       file.writelines(sheets) 
       file.write('\n') 
+0

在for循環中添加一個打印線,寫出線條,你會發現它。也意識到每次打開它時文件都會被截斷。 –

+0

我已經打印出行,但它仍然不清楚發生了什麼事情,以獲得.csv結果? –

+0

'sheets'是一條線。 '對於表單中的l:'遍歷行中的項目,但'l'從不使用。 'file.writelines'對於單行不正確。 'file.write('\ n')'不需要。 'csv'將管理這些行。你不知道你爲什麼沒有在你的csv中得到任何逗號嗎? –

回答

1

您可能想要更類似於以下未經測試的代碼。提供的示例不能按原樣運行:

from bs4 import BeautifulSoup 
import requests 
import csv 

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

# Open the file once. See the csv documentation for the correct way to open 
# a file for use with csv.writer. If you plan to open the .csv with 
# Excel, the utf-8-sig encoding will allow non-ASCII to work correctly. 
with open('csvfile.csv','w', encoding='utf-8-sig', newline='') as f: 
    file = csv.writer(f) # actually use the CSV module. 

    for i in range(1, 300): 
     url = "xxx?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] 

      # write a single line. 
      file.writerow(sheets) 

這是一個在Excel中打開的測試示例。我扔在非ASCII字符,並在數據逗號證明csv模塊的處理它的能力:

#coding:utf8 
import csv 

with open('csvfile.csv','w', encoding='utf-8-sig', newline='') as f: 
    file = csv.writer(f) 
    file.writerow('BRAND PRODUCT STOCK PRICE'.split()) 
    for i in range(1,11): 
     sheets = ['brand{}'.format(i),'pröduct{}'.format(i),'st,ock{}'.format(i),'price{}'.format(i)] 
     file.writerow(sheets) 

輸出:

BRAND,PRODUCT,STOCK,PRICE 
brand1,pröduct1,"st,ock1",price1 
brand2,pröduct2,"st,ock2",price2 
brand3,pröduct3,"st,ock3",price3 
brand4,pröduct4,"st,ock4",price4 
brand5,pröduct5,"st,ock5",price5 
brand6,pröduct6,"st,ock6",price6 
brand7,pröduct7,"st,ock7",price7 
brand8,pröduct8,"st,ock8",price8 
brand9,pröduct9,"st,ock9",price9 
brand10,pröduct10,"st,ock10",price10 

在Excel:

Excel image

+0

一旦所有行都寫入文件,是否有方法可以打印確認信息? –