ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xca in position 339: invalid continuation byte

2021-11-29 14:34:03  阅读:431  来源: 互联网

标签:339 request urllib decode html import utf byte


目录

源代码

出现的错误

解决方法

问题原因

参考文献


在爬微博,解析网页的时候

源代码

import urllib.request
from bs4 import BeautifulSoup
import re
import urllib.request,urllib.error
import xlwt
import requests     #数据抓取库


#1.数据抓取
url = 'https://s.weibo.com/top/summary'
headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.44',
        'Referer': 'https://detail.tmall.com/item.htm?id=539845300741&rn=014fb1b89d7688b86780dbfa0d5037ee&abbucket=0',
         }
request=urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request)
html = response.read().decode('UTF-8')
#print(response.text)



#2.数据解析

soup = BeautifulSoup(html, "html.parser")

出现的错误

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xca in position 339: invalid continuation byte

解决方法

将'UTF-8'换为'ANSI'

html = response.read().decode('ANSI')

问题原因

原页面的编码方式为:ANSI


参考文献

python 报错"UnicodeDecodeError: 'utf-8' codec can't decode byte"的解决办法_米兰小子SHC-CSDN博客

标签:339,request,urllib,decode,html,import,utf,byte
来源: https://blog.csdn.net/My_progress1/article/details/121608926

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有