UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 30073: invalid continuation byt

出错代码:

    @retry(stop_max_attempt_number=10)
    def _get_url_content(self, start_url):

        proxies = get_proxies_requests(start_url)
        random_header = get_header()
        add_header = {

                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
                'Accept-Encoding': 'gzip, deflate',
                'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
                'Cache-Control': 'max-age=0',
                'Connection': 'keep-alive',
                'Host': 'www.chinanews.com',
                'Referer': start_url,
                'Upgrade-Insecure-Requests': '1',

        }

        last_header = dict()
        last_header.update(random_header)
        last_header.update(add_header)
        html = requests.get(start_url, headers=last_header, proxies=proxies, timeout=10, allow_redirects=False)
        assert html.status_code == 200
        return html

 

调用代码:

            try:
                html = self._get_url_content(start_url=start_url)
            except:
                html = ''
            if html is not '':
                html_str = html.content.decode('utf8')
                

报错为:

H:\python3.5.2\python.exe F:/shining_future/spider_bogger/a044_news_spider/a14_chinanews_spider/chinanews_v1.py
爬虫启动
200
Exception in thread Thread-2:
Traceback (most recent call last):
  File "H:\python3.5.2\lib\threading.py", line 914, in _bootstrap_inner
    self.run()
  File "H:\python3.5.2\lib\threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "F:/shining_future/spider_bogger/a044_news_spider/a14_chinanews_spider/chinanews_v1.py", line 223, in get_url_content
    list_page_news_lists = self.get_data_from_response(orginal_html=html, year=year, month_today=month_today)
  File "F:/shining_future/spider_bogger/a044_news_spider/a14_chinanews_spider/chinanews_v1.py", line 92, in get_data_from_response
    html_str = orginal_html.content.decode('utf8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 30073: invalid continuation byte

处理办法:

html_str = html_str.content.decode('utf8', 'ignore')

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UnicodeDecodeError是一种编码错误,它表示在解码过程中出现了无法解码的字节。具体来说,在你提供的引用中,出现了一些关于UTF-8编码无法解码字节的错误。[4] 通常,这种错误的原因可能是以下之一: 1. 编码错误:字节序列不符合指定的编码格式。UTF-8编码使用多个字节来表示字符,如果字节序列不符合UTF-8编码规则,就会出现解码错误。 2. 文件损坏:如果文件本身被损坏,其中的字节可能无法正确解码。 3. 使用错误的编码格式:在解码时使用了错误的编码格式,导致无法正确解码。 为了解决这个问题,你可以尝试以下几种方法: 1. 检查编码格式:确保你正在使用正确的编码格式进行解码。UTF-8是一种常用的编码格式,但也有可能是其他编码格式,比如GBK等。 2. 使用特定的解码错误处理方式:在解码过程中,可以使用ignore参数来忽略无法解码的字节,以避免抛出错误。例如,使用decode('utf-8', 'ignore')来忽略无法解码的字节。 3. 检查文件完整性:如果错误发生在文件上,可以尝试重新下载文件或使用备份文件来确保文件完整性。 需要注意的是,具体解决方法取决于你遇到问题的具体情况,可能需要根据错误提示和上下文进行进一步的调查和分析。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值