我正在编写一个程序,该程序将从网站获取七条数据,并将其写入符号.txt 文件中每个公司的 csv 文件,例如 AAPL 或 NFLX。我的问题似乎来自我对索引的困惑,以使脚本工作。我不知道它是如何适合的。我认为这种格式会起作用......
import urllib2
from BeautifulSoup import BeautifulSoup
import csv
import re
import urllib
# import modules
symbolfile = open("symbols.txt")
symbolslist = symbolfile.read()
newsymbolslist = symbolslist.split("\n")
i = 0
f = csv.writer(open("pe_ratio.csv","wb"))
# short cut to write
f.writerow(["Name","PE","Revenue % Quarterly","ROA% YOY","Operating Cashflow","Debt to Equity"])
#first write row statement
# define name_company as the following
while i<len(newsymbolslist):
page = urllib2.urlopen("http://finance.yahoo.com/q/ks?s="+newsymbolslist[i] +"%20Key%20Statistics").read()
soup = BeautifulSoup(page)
name_company = soup.findAll("div", {"class" : "title"})
for name in name_company: #add multiple iterations?
all_data = soup.findAll('td', "yfnc_tabledata1")
stock_name = name.find('h2').string #find company's name in name_company with h2 tag
f.writerow([stock_name, all_data[2].getText(),all_data[17].getText(),all_data[13].getText(), all_data[29].getText(),all_data[26].getText()]) #write down PE data
i+=1
当我尝试按原样运行代码时,出现以下错误:
Traceback (most recent call last):
File "company_data_v1.py", line 28, in <module>
f.writerow([stock_name, all_data[2].getText(),all_data[17].getText(),all_data[13].getText(), all_data[29].getText()
all_data[26].getText()]) #write down PE data
IndexError: list index out of range
提前感谢您的帮助。