I have a set of urls (stock data) for which I want certain data to be put into a csv. Per row I need to have:
name price recrat opinion
A csv appears but has no data, and I get the error:
ValueError: too many values to unpack
How should I go about this? Here is my code so far:
# -*- 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
is the list with all those links in it (as shown at the top of your code), what values arename
andprice
supposed to take as you iterate through them? – khelwood Oct 1 '17 at 8:35