这个程序是webscraping的一个非常简单的例子。该计划的目标是上网,找到特定的股票,然后告诉用户该股票当前交易的价格。但是,我在代码中遇到了这个问题,当我编译它时,会出现此错误消息:
Traceback (most recent call last):
File "HTMLwebscrape.py", line 15, in <module>
price = re.findall(pattern,htmltext)
File "C:\Python34\lib\re.py", line 210, in findall
return _compile(pattern, flags).findall(string)
TypeError: can't use a string pattern on a bytes-like object
以下是该程序的实际脚本。我已经尝试过找到解决此问题的方法,但到目前为止,我还是无法解决。我已经在Python 3上运行它并使用Submlime Text作为我的文本编辑器。提前谢谢!
import urllib
import re
from urllib.request import Request, urlopen
from urllib.error import URLError
symbolslist = ["AAPL","SPY","GOOG","NFLX"]
i=0
while i < len(symbolslist):
url = Request("http://finance.yahoo.com/q?s=AAPL&ql=1")
htmlfile = urlopen(url)
htmltext = htmlfile.read()
regex = '<span id="yfs_184_'+symbolslist[i] + '"">(.+?)</span>'
pattern = re.compile(regex)
price = re.findall(pattern,htmltext)
print (price)
i+=1