Get the App
SLTechnology News&Howtos  ›  Development  › 

The difference between response.text and response.content in request and how to use it

Shulou Source: shulou.com Published: 2022-06-01 12:28:36 10月04日 Update

This article mainly explains "the difference and how to use response.text and response.content in request". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "the difference and how to use response.text and response.content in request".

1.response.text

-Type: str

-Decoding type: speculated text encoding based on the encoding of the response in the HTTP header.

-how to modify the encoding method: response.encoding= "gbk"

2. Response.content

-Type: bytes

-Decoding type: not specified

-how to modify the encoding method: response.content.deocde ("utf-8")

3. A common way to get the source code of a web page: response.content.decode () response.content.decode ("GBK")

The decoding method can be determined by finding Content-Type:text/html;charset=utf-8 in the response header or content= "text/html;charset=utf-8'' in the source code of the web page.

Response.text

The above three methods can solve the problem of decoding all web pages 100%.

Therefore: it is more recommended to use * * response.content.deocde () * * to get the html page of the response

Supplement: response is commonly used in requests in python3

Look at the code ~ import requestsres = requests.get ("http://127.0.0.1:9092")print(res.status_code) # 200print (res.url) # http://127.0.0.1:9092/print(res.headers) # {'Content-Type':' application/json Charset=utf-8', 'Content-Length':' 10 minutes, 'Date':' Sat, 22 Dec 2018 13:36:16 GMT' 'Connection':' keep-alive'} print (res.cookies) # print (res.text) # 8248154254print (res.content) # baked 8248154254' write picture print (res.cookies ['cid']) # hello world# crawl picture R1 = requests.get (' https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545496551516&di=8ed19596bc72aa87299ed7f234830128&imgtype=0&src=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201107%2F31%2F20110731155631 _ htMcs.jpg') b = r1.contentwith open ('hao.jpg' 'wb') as f: f.write (b) Thank you for your reading The above is the content of "the difference and how to use response.text and response.content in request". After the study of this article, I believe you have a deeper understanding of the difference between response.text and response.content in request and how to use this problem. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Mode type coding web page learning content picture source code question code header that is commonly used ideas situations text articles methods more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi OPPO Reno vpn Microsoft