爬虫出现TypeError: cannot use a string pattern on a bytes-like object报错
程序员文章站
2024-01-03 18:20:28
...
import urllib.request
import re
data = urllib.request.urlopen("https://edu.csdn.net/huiyiCourse/detail/253").read()
pat = "<p>(\d*?)</p>"
rst = re.compile(pat).findall(data)
print(rst)
爬虫出现报错:TypeError: cannot use a string pattern on a bytes-like object
原因:解码问题报错
解决方案:
需要在对应的数据中加上 .decode(“utf-8”)
data = urllib.request.urlopen("https://edu.csdn.net/huiyiCourse/detail/253").read().decode("utf-8")