2017-09-21 5 views
2

ここではPython write to CSV line by lineに記載されている方法のいずれかを使用して、出力のすべての行を.CSVに書き込もうとしました。私はそれをCSVの出力と生成の段階に持っていきましたが、私のデータのすべての行を表示するのではなく、4回繰り返された1行を見ています。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'は1行です。 'for l sheets:'は行中の項目を繰り返しますが、 'l'は決して使用されません。 'file.writelines'は1行では正しくありません。 '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で開くテスト済みのサンプルを示します。私はそれを処理するために、csvモジュールの能力を実証するために、非ASCII文字やデータにカンマで投げた:

#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) 

出力:Excelで

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 image

+0

すべての行がファイルに書き込まれたら、確認メッセージを印刷する方法はありますか? –

関連する問題