相关疑难解决方法(0)

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

我正在尝试学习如何从页面自动获取网址.在下面的代码中,我试图获取网页的标题:

import urllib.request
import re

url = "http://www.google.com"
regex = r'<title>(,+?)</title>'
pattern  = re.compile(regex)

with urllib.request.urlopen(url) as response:
   html = response.read()

title = re.findall(pattern, html)
print(title)
Run Code Online (Sandbox Code Playgroud)

我得到了这个意想不到的错误:

Traceback (most recent call last):
  File "path\to\file\Crawler.py", line 11, in <module>
    title = re.findall(pattern, html)
  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
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

python web-crawler python-3.x

90
推荐指数
2
解决办法
12万
查看次数

标签 统计

python ×1

python-3.x ×1

web-crawler ×1