因此,我一直在编写一个简单的脚本,从项目主目录中的.txt文件中提取股票符号,但我似乎无法将其带回定价数据。如果我手动将它们输入到字符串数组中,这是有效的,但当涉及到从文件中提取时,我只是不想返回价格。
import urllib
import re
symbolfile = open("symbols.txt")
symbolslist = symbolfile.read()
newsymbolslist = symbolslist.split("n")
i = 0
while i<len(newsymbollist):
url = "http://finance.yahoo.com/q?uhb=uh3_finance_vert_gs_ctrl1&fr=&type=2button&s=" +symbolslist[i] +""
htmlfile = urllib.urlopen(url)
htmltext = htmlfile.read()
regex = '<span id="yfs_184_' +newsymbolslist[i] +'">(.+?)</span>'
pattern = re.compile(regex)
price = re.findall(pattern,htmltext)
print "The price of", newsymbolslist[i] ," is ", price
i+=1
我真的需要一些帮助,因为它没有在外壳中给出任何错误。
提前感谢您的帮助!
通过实现@Linus Gustav Larsson Thiel在注释中提供的修改以及另一个关于regex
的修改,您的代码将返回正确的结果。请注意正则表达式中的lowercase()
,因为源代码包括小写符号:
i = 0
while i < len(newsymbolslist):
url = "http://finance.yahoo.com/q?uhb=uh3_finance_vert_gs_ctrl1&fr=&type=2button&s=" +newsymbolslist[i]
htmlfile = urllib.urlopen(url)
htmltext = htmlfile.read()
regex = '<span id="yfs_l84_' +newsymbolslist[i].lower() +'">(.+?)</span>'
pattern = re.compile(regex)
price = pattern.findall(htmltext)
print "The price of", newsymbolslist[i] ," is ", price
i+=1
通过用于测试目的的静态列表['AAPL','GOOGL','MSFT']
,我收到以下输出:
The price of AAPL is ['98.53']
The price of GOOGL is ['733.07']
The price of MSFT is ['52.30']
如果你愿意,你也可以简化你的代码:
baseurl = "http://finance.yahoo.com/q?uhb=uh3_finance_vert_gs_ctrl1&fr=&type=2button&s="
for symbol in newsymbolslist:
url = baseurl + symbol
source = urllib.urlopen(url).read()
regex = re.compile('<span id="yfs_l84_' + symbol.lower() + '">(.+?)</span>')
price = regex.findall(source)[0]
print "The price of", symbol, "is", price
for ... in ...
循环消除了对计数器变量的需要,并且由于findall()
返回一个匹配列表(而您只期望一个),因此可以附加[0]
来显示包含字符串,而不是带有单个元素的列表。
这将返回以下内容:
The price of AAPL is 98.53
The price of GOOGL is 733.07
The price of MSFT is 52.30