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')

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值