贴吧爬虫出现错误:ValueError: Unicode strings with encoding declaration are not supported. Please use bytes i

一、原因

贴吧爬虫出现错误:ValueError: Unicode strings with encoding declaration are not supported. Please use bytes i_第1张图片

    def get_page_from_url(self, url):
        response = requests.get(url, headers=self.headers)
        # 这个位置可能会有问题(去掉了decode())
        # return response.content
        return response.content.decode()
.......
    def get_datas_from_page(self, page):
        '''提取页面中数据'''
        # 把page转换为Element对象
        # ValueError: Unicode strings with encoding declaration are not supported.
        # 解决方案; 就是要传入二进制数据
        html = etree.HTML(page)

二、解决方案:

    def get_page_from_url(self, url):
        response = requests.get(url, headers=self.headers)
        # 这个位置可能会有问题(去掉了decode())
        return response.content
        # return response.content.decode()

    def get_datas_from_page(self, page):
        '''提取页面中数据'''
        # 把page转换为Element对象
        # ValueError: Unicode strings with encoding declaration are not supported.
        # 解决方案; 就是要传入二进制数据
        html = etree.HTML(page)
      ........

 

你可能感兴趣的:(编程学习)