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

How to solve the problem of response attribute of request request in Python

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

Share

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

Today, the editor will share with you the relevant knowledge points about how to solve the response attribute problem requested by request in Python. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

The response obtained by the request request in Python, that is, the data obtained through request:

Import requestsresponse = requests.get (attribute 1 of https://www.jd.com/)response, return status code response.status_code

The returned status of the http request. 2XX: successful connection; 3XX: jump; 4XX client error; 500server error

2. Return the text content response.text of the http response

The str form of the http response content, which requests the page content corresponding to the url

Response=requests.get ("https://www.jd.com/")print(response.text)"

If there is garbled code in the printing process

You can use encoding to modify the encoding format:

Response.encoding= "utf-8" print (response.text)

3. Return the binary data of the http response

Response = requests.get ("https://www.jd.com/")# print (response.content) # prints out the binary form print (response.content.decode (" utf-8 "))

Summary:

Compare response's text method with response's content method:

Response.text returns Unicode data, while response.content returns bytes, that is, binary data.

Take the text with the method of .text and the picture with the method of .content

4. The response content encoding method response.encoding5 guessed from HTTP header, the response content encoding mode analyzed from the content (alternative encoding method) response.apparent_encoding6, http response content response.headers

Supplement: python crawler requests module (response common attributes)

Response Common Properties

Page data of binary (byte) type in response object obtained by content

Response.content

Returns the response status code

Response.status_code

two hundred

Return response header information

Response.headers

Get request url

Response.url

These are all the contents of this article entitled "how to solve the problem of response attributes requested by request in Python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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