栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

目录

源代码

出现的错误

解决方法

问题原因

参考文献


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

源代码
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博客

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/619360.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号