日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【Python】处理UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xa2 in position…

發布時間:2025/3/15 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python】处理UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xa2 in position… 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

編碼格式問題

Traceback (most recent call last):
??File “D:/PyCharm/text_processing/解析并清洗HTML.py”, line 6, in <module>
????lines = f.readlines()
UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xa2 in position 216: illegal multibyte sequence


遇到此情況,就是編碼格式的問題了,我們應該設置編碼格式為UTF-8:
with open(‘test.html’, ‘r’, encoding=‘UTF-8’) as f:

bs4.FeatureNotFound

Traceback (most recent call last):
??File “D:/PyCharm/text_processing/解析并清洗HTML.py”, line 11, in <module>
soup = BeautifulSoup(string, “lxml”)
??File “D:\PyCharm\text_processing\venv\lib\site-packages\bs4\__init__.py”, line 228, in __init__
????% “,”.join(features))
bs4.FeatureNotFound: Couldn’t find a tree builder with the features you requested: lxml. Do you need to install a parser library?


這個問題的產生原因是缺少lxml庫,可以pip一下:pip3 install lxml

測試代碼

from bs4 importstring = ""with open('test.html', 'r', encoding='UTF-8') as f:lines = f.readlines()for i in lines:string = '%s %s' % (string, i)# 解析HTML soup = BeautifulSoup(string, "lxml")# 查找id為"b_id"的div標簽,并查看文本 print(soup.find("div", {"id": "b_id"}).text) <!DOCTYPE html> <html lang="ch"> <head><meta charset="UTF-8"><title>測試頁面</title> </head> <body><div id="a_id"><div id="b_id"><div id="c_id">大碗寬面,真香!</div></div></div> </body> </html> 大碗寬面,真香! <!DOCTYPE html> <html lang="ch"> <head><meta charset="UTF-8"><title>測試頁面</title> </head> <body><div id="a_id"><div id="b_id"><div id="c_id">大碗寬面,真香!</div></div></div> </body> </html> 大碗寬面,真香! <!DOCTYPE html> <html lang="ch"> <head><meta charset="UTF-8"><title>測試頁面</title> </head> <body><div id="a_id"><div id="b_id"><div id="c_id">大碗寬面,真香!</div></div></div> </body> </html> 大碗寬面,真香!

可見這東西與文檔格式有關,它會關注到空白字符,和瀏覽器解析顯示不同。

總結

以上是生活随笔為你收集整理的【Python】处理UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xa2 in position…的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。