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