编写爬虫时遇到报错:
“TypeError: cannot use a string pattern on a bytes-like object”
百度了一下,出现缘由是python3中request.urlopen().read()返回的内容类型不是string,而是byte,因此须要在正则匹配时对被匹配对象解码
将python
links=re.findall('<loc>(.*?)</loc>',sitemap)
改成url
links=re.findall('<loc>(.*?)</loc>',sitemap.decode('utf-8'))