[英]TypeError: can't concat bytes to str
我正在嘗試作為教程的一個例子。 此代碼的目的是讀取URL,為公司列表提取股票值並打印該值。 我有什么,試圖拼湊一些建議其他帖子,原始代碼(Python 3)是:
import urllib.request
import re
Symbols = ['aapl', 'spy' , 'goog' , 'nflx']
i = 0
while i < len(Symbols):
Yahoo='http://finance.yahoo.com/q?s=' + Symbols[i]
htmlfile = urllib.request.urlopen(Yahoo)
htmltext = htmlfile.read()
pattern= re.compile(b'<span id="yfs_l84_'+ Symbols[i] +'">(.+?)</span>')
price= re.findall(pattern, htmltext)
print('The price of' + (Symbols[i]) + ' is ' + str(price))
i+=1
我知道html.read()的輸出是以字節為單位的,所以我需要將我的正則表達式模式轉換為'bytes'(使用'b'),然后我得到的錯誤信息是:
Traceback (most recent call last):
File "C:/Users/User/Documents/Raspberry Pi/Python/web scraper/web_scraper_v2.1.py", line 11, in
price= re.findall(pattern, htmltext)
File "C:\Python33\lib\re.py", line 201, in findall
return _compile(pattern, flags).findall(string)
TypeError: can't use a string pattern on a bytes-like object
我懷疑這是語法相關的,但無法解決任何建議嗎?
干得好:
import urllib.request
import re
Symbols = ['aapl', 'spy', 'goog', 'nflx']
i = 0
while i < len(Symbols):
Yahoo = 'http://finance.yahoo.com/q?s=' + Symbols[i]
htmlfile = urllib.request.urlopen(Yahoo)
htmltext = htmlfile.read()
# Changed the string below so that we can resolve the problems with putting the
# symbol value in the right place. str.format has been used instead
# Also, the string has been made into a normal string again
pattern = re.compile('<span id="yfs_l84_{symbol}">(.+?)</span>'.format(symbol=Symbols[i]))
# Here htmltext is turned into a string so that we can compare, without the type-error
price = re.findall(pattern, str(htmltext))
print('The price of' + (Symbols[i]) + ' is ' + str(price))
i += 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.