我正在尝试收集四只股票的市盈率。我不确定我哪里出错了,感谢您的帮助。
我认为问题在于我的贪婪和非贪婪限定符以及正则表达式 url 的复制方式。
import urllib
import re
symbolslist = ["aapl","spy","goog","nflx"]
i=0
while i<len(symbolslist):
url = "http://finance.yahoo.com/q?s=" +symbolslist[i] +"&q1=1"
htmlfile = urllib.urlopen(url)
htmltext = htmlfile.read()
regex = '<th scope="row" width="48%">"P/E "<span class="small">(ttm)</span>: </th><td class="yfnc_tabledata1">(.+?)</td>'
pattern = re.compile(regex)
price_to_earnings = re.findall(pattern,htmltext)
print "The price to earnings of", symbolslist[i]," is", price_to_earnings
i+=1