我是 python 新手,所以我为任何新手错误道歉。我按照教程从 python 中抓取股票价格,但在修复它以在 python 3 中工作后,当我尝试将其调整到雅虎财经页面的其他元素(例如市盈率和 Beta)时,输出只是空方括号。
import urllib.request
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.request.urlopen(url)
htmltext = htmlfile.read()
regex = b'<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 = str(re.findall(pattern,htmltext))
print ("The price to earnings of " + symbolslist[i]+ " is " + price_to_earnings)
i+=1
这是输出
The price to earnings of aapl is []
The price to earnings of spy is []
The price to earnings of goog is []
The price to earnings of nflx is []
>>>