Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report