TypeError: can't use a string pattern on a bytes-like object:
在读取网页内容时报了上面这个错误,python2.7版本不报错,python3.5的就报错了
from urllib.request import urlopen
webpage = urlopen('http://www.python.org')
import re
text = webpage.read()
m = re.search('<a href="([^"]+)" .*?>about</a>', text, re.IGNORECASE)
print(m.group(1))
于是在网上找解决方案,说
Generally when things are passed around the network, they're in the form of bytes
大意就是说在网络上传输文件都是以二进制格式传输的,所以需要对读取的网页内容进行解码
将
text = webpage.read()
替换成
text = webpage.read().decode()