re.search().TypeError: 不能在类似字节的对象上使用字符串模式

thi*_*uan 5 python python-3.x

import urllib.request
import re

f = urllib.request.urlopen('http://www.geekynu.cn/')
html = f.read()

title = re.search('<title>(.*?)</title>', html)
print(title)
#print(title.decode('utf-8')) //I had try to solve by this code.
Run Code Online (Sandbox Code Playgroud)

[python 3.5] 当我使用re.search()阅读网页标题时,出现错误“TypeError: cannot use a string pattern on a bytes-like object”,我该怎么办?谢谢!

Mos*_*oye 8

re需要字节模式(不是字符串)来搜索类似字节的对象。将 a 添加b到您的搜索模式,如下所示:b'<title>(.*?)</title>'


小智 5

如果你可以添加html=html.decode('utf-8'),我想就可以了。