【发布时间】:2014-03-31 16:38:28
【问题描述】:
我正在使用 Python 3.2.3 运行此代码:
regex = '<title>(.+?)</title>'
pattern = re.compile(regex)
然后使用 findall 搜索模式:
titles = re.findall(pattern,html)
print(titles)
html 对象从特定的 url 获取 html 代码。
html = response.read()
我收到错误“不能在类似字节的对象上使用字符串模式”。我试过使用:
regex = b'<title>(.+?)</title>'
但这会在我的结果中附加一个“b”吗?谢谢。
【问题讨论】:
-
html对象是什么?尝试使用str(html)。会发生什么? -
你推荐哪个 Python 的 HTML 解析器 Ignacio?
标签: python regex string compilation byte