2017-10-01 1 views
0

私はあるデータをcsvに入れるための一連のURL(株データ)を持っています。行ごとに私が持っている必要があります:pythonのループをURLの配列に渡し、csvの行ごとにデータを書き込む方法はありますか?

name price recrat opinion 

CSVが表示されますが、データがない、と私はエラーを取得:

ValueError: too many values to unpack 

は、どのように私はこれについて行かなければなりませんの?私はresultsにこれを名前を変更: - ここに私のコードは、これまでに次のとおりです:

# -*- coding: utf-8 -*- 
import urllib2 
from bs4 import BeautifulSoup 
import csv 
from datetime import datetime 

quote_page = ['http://uk.mobile.reuters.com/business/quotes/overview/AALB.AS', 
    'http://uk.mobile.reuters.com/business/stocks/overview/ABNd.AS', 
    'http ://uk.mobile.reuters.com/business/stocks/overview/ACCG.AS', 
    'http ://uk.mobile.reuters.com/business/stocks/overview/AD.AS'] 


for link in quote_page: 
    try: 
     page = urllib2.urlopen(link) 
     soup = BeautifulSoup(page, 'html.parser') 

     name_box = soup.find('span', attrs={'class': 'company-name'}) 
     name = name_box.text.strip() 
     print name 

     price_box = soup.find('span', attrs={'class':'price'}) 
     price = price_box.text.strip() 
     print price 

     recrating_box = soup.find('div', attrs={'class':'recommendation-rating'}) 
     recrat = recrating_box.text.strip() 
     print recrat 

     opinion = soup.find('div', attrs={'class':'recommendation-marker'})['style'] 
     print opinion 
    except TypeError: 
     continue 

quote_page.append((name, price, recrat, opinion)) 
    # open a csv file with append, so old data will not be erased 
with open('index.csv', 'a') as csv_file: 
    writer = csv.writer(csv_file) 
    for name, price in quote_page: 
     writer.writerows([name, price, recrat, opinion, datetime.now()]) 
+1

'quote_page'が(コードの先頭に表示されているように)その中のすべてのリンクを持つリストであれば、' name'と 'price'はあなたがそれらを反復する際に取るべき値ですか? – khelwood

答えて

1
テスト済み

と動作していない良いアイデア

# -*- coding: utf-8 -*- 
import urllib2 
from bs4 import BeautifulSoup 
import csv 
from datetime import datetime 

quote_page = ['http://uk.mobile.reuters.com/business/quotes/overview/AALB.AS', 
    'http://uk.mobile.reuters.com/business/stocks/overview/ABNd.AS', 
    'http://uk.mobile.reuters.com/business/stocks/overview/ACCG.AS', 
    'http://uk.mobile.reuters.com/business/stocks/overview/AD.AS'] 

results = [] 

for link in quote_page: 
    try: 
     page = urllib2.urlopen(link) 
     soup = BeautifulSoup(page, 'html.parser') 

     name_box = soup.find('span', attrs={'class': 'company-name'}) 
     name = name_box.text.strip() 
     print name 

     price_box = soup.find('span', attrs={'class':'price'}) 
     price = price_box.text.strip() 
     print price 

     recrating_box = soup.find('div', attrs={'class':'recommendation-rating'}) 
     recrat = recrating_box.text.strip() 
     print recrat 

     opinion = soup.find('div', attrs={'class':'recommendation-marker'})['style'] 
     print opinion 
    except TypeError: 
     continue 

    results.append((name, price, recrat, opinion)) 

# open a csv file with append, so old data will not be erased 
with open('index.csv', 'w') as csv_file: 
    writer = csv.writer(csv_file) 
    for item in results: 
     writer.writerow([item[0], item[1], item[2], item[3], datetime.now()]) 

3つの問題、まず、あなたがアクティブリストを上書きしたがありました。

第2に、リストを繰り返し処理していましたが、4つの項目のうち2つのみにアクセスしようとしていました。私はこれらを索引付けしました。

最後に、反復処理を行っているときに、行単位で処理したいので、writerowswriterowに変更する必要があります。

+0

これはcsvを作成しますが、最初の配列項目のすべての文字を別のセルに置きます。 ([名、価格、recrat、意見、datetime.now()]) writer.writerowsに ファイル "45.py"、ライン36、: トレースバック(最後の最新の呼び出し):とエラーになります_csv.Error:シーケンスが期待される –

+0

は私の答えを更新し、今回はそれをテストしました - 魅力のように働きます:) – Adders

+0

詳細を今すぐ追加します – Adders

関連する問題