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

歡迎訪問 生活随笔!

生活随笔

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

python

python requests 乱码_解决使用requests中文乱码

發布時間:2023/12/20 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python requests 乱码_解决使用requests中文乱码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

碰到了使用requests亂碼,總結一下。

Requests will automatically decode content from the server. Most unicode charsets are seamlessly decoded.

When you make a request, Requests makes educated guesses about the encoding of the response based on the HTTP headers. The text encoding guessed by Requests is used when you access r.text. You can find out what encoding Requests is using, and change it, using the r.encoding property:

>>> r.encoding 'utf-8' >>> r.encoding = 'ISO-8859-1'

If you change the encoding, Requests will use the new value of r.encoding whenever you call r.text. You might want to do this in any situation where you can apply special logic to work out what the encoding of the content will be. For example, HTML and XML have the ability to specify their encoding in their body. In situations like this, you should use r.content to find the encoding, and then set r.encoding. This will let you use r.text with the correct encoding.

Requests will also use custom encodings in the event that you need them. If you have created your own encoding and registered it with the codecs module, you can simply use the codec name as the value of r.encoding and Requests will handle the decoding for you.

亂碼原因:假設頁面編碼方式為utf-8,如果服務器返回的response header沒有charset=utf-8選項,requests 默認的encode方式為 ISO-8859-1

例子

百度網頁頁面用的的是utf-8編碼(在頁面里可找到),百度response header里沒有設置charset-utf-8,requests response默認采用ISO-8859-1編碼,因此引起中文亂碼

淘寶網頁頁面用的的是utf-8編碼(在頁面里可找到),淘寶response header有設置charset-utf-8,requests response默認采用header中的編碼utf-8編碼,因此中文沒有亂碼

簡單解決方式1: 重新編碼解碼 baidu.text.encode('ISO-8859-1').decode('utf-8')

簡單解決方式2 baidu.encoding='utf-8'

簡單解決方式3:baidu.content type為字節流,即是編碼后的字節流,用utf-8解碼得到中文無亂碼

總結

以上是生活随笔為你收集整理的python requests 乱码_解决使用requests中文乱码的全部內容,希望文章能夠幫你解決所遇到的問題。

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