我今天在學習Python用代理ip 訪問網頁的時候 報了以上錯誤 在網上找了幾種方法不是崩潰就是報錯 比如這種:
html = response.read().decode('utf-8',‘ignore’) 在這句話后面加 ‘ignore’ 感覺好暴力 但是崩潰了
最后我想到直接用 ‘GBK’編碼模式就好啦
html = response.read().decode('GBK') 完美運行 沒有報錯
最后附上完整代碼:
import urllib.request
url = 'http://www.ip138.com'
proxy_support = urllib.request.ProxyHandler({'http':'219.141.153.41:80'})
opener = urllib.request.build_opener(proxy_support)
opener.addheaders = [('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36')]
urllib.request.install_opener(opener)
response = urllib.request.urlopen(url)
html = response.read().decode('GBK')
print(html)
————————————————
版權聲明:本文為CSDN博主「蠟筆小悠悠」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_40268727/java/article/details/81585669