我正在從一個教程,從雅虎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