我正在从一个教程,从雅虎Fiances拉不同的股票价格。我有这个代码,它的工作原理是打印出不同股票代码的价格,然后是数组括号,但不是价格。所有帮助非常感谢。Python获取股票价格
import urllib
import re
symbolslist = ["aapl", "spy", "goog", "nflx"]
i = 0
while i < len(symbolslist):
url = "http://finance.yahoo.com/q?s=" + symbolslist[i] + "&ql=1"
htmlfile = urllib.urlopen(url)
htmltext = htmlfile.read()
regex = '<span id="yfs_l84_' + symbolslist[i] + ' "> (.+?) </span>'
pattern = re.compile(regex)
price = re.findall(pattern, htmltext)
print "the price of ", symbolslist[i], " is ", price
i += 1
有可能雅虎在编写代码后更改了他们的HTML。您是否仔细检查了它所寻找的跨度是否仍然存在? – redtuna
为什么不使用for循环; symbolslist:'? –
你的正则表达式有一个额外的空间e关闭'''在你的价格之前和之后 – cmd