UnicodeDecodeError: 'gbk' codec can't decode byte 0xd0 in position 13345: illegal multibyte sequence


1 import requests 2 
3 headers = { 4     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
5 } 6 url = "https://www.dytt8.net/html/gndy/dyzz/list_23_2.html"
7 response = requests.get(url, headers=headers) 8 text = response.content.decode(encoding='gbk') 9 print(text)

運行報錯,

 

查看頁面是有無法識別的內容

百度試過更換編碼gb18030,可是還是會報錯.

最后找到了解決的方法,在decode方法中有errors參數

如下:

text = response.content.decode(encoding='gbk', errors='ignore')

程序就不報錯正常執行了.

百度中找到個方法打開網頁:F12 在console輸入document.charset 查看編碼方式

 

當然,其實在這個程序中不需要提取這些內容,直接使用response.text()也可以.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM