Python requests

text vs content in requests.models.Response

In the Python requests library, the Response object has two attributes called text and content.

The text attribute contains the web page content retrieved from the network, and it returns the content as a string. It also decodes all of the characters in the web page content so that it can be processed in the program.

The content attribute also contains the web page content retrieved from the network, but it returns the content as a byte stream, which means it doesn't decode the content. This is useful for working with binary data, such as images or audio files.

So, if you want to process the web page content and want to work with it as a string, you should use the text attribute. If you want to work with binary data, you should use the content attribute.

 head method in requests library

In the Python requests library, the head method is used to send a HEAD request.

A HEAD request is similar to a GET request in that it is used to retrieve the content of a web page. However, the difference is that a HEAD request only requests the header information of the web page, and not the content.

This is useful for checking the last modified time of a web page, or the content type of the page, among other things. Because it only requests the header information of the web page, it is usually much faster than sending a GET request to retrieve the entire web page content.

Here is an example of using the head method to send a HEAD request:

 
import requests response = requests.head("https://www.python.org/") print(response.headers)

The code above sends a HEAD request to https://www.python.org/ and prints the header information of the web page.

json method in requests

In the Python requests library, the json method is used to parse a JSON-formatted response.

JSON (JavaScript Object Notation) is a popular data format that is used for transmitting data over the internet. It is a lightweight, human-readable format that is easy to understand and work with.

When you make a request to an API that returns JSON data, you can use the json method to parse the JSON-formatted response and convert it into a Python dictionary. This allows you to easily access the individual data items in the response and work with them in your program.

Here is an example of using the json method to parse a JSON-formatted response:

import requests

response = requests.get("https://api.github.com/users/octocat")

data = response.json()

print(data)

The code above makes a request to the GitHub API to get information about the user "octocat", and then uses the json method to parse the response and convert it into a Python dictionary. The resulting dictionary is then printed to the console.

You can also use the json method to convert a Python dictionary into a JSON-formatted string by passing the dictionary to the method as an argument. This can be useful if you want to send a JSON-formatted request to an API, or if you want to save the data to a file in JSON format.

Here is an example of using the json method to convert a Python dictionary into a JSON-formatted string:

 

import requests

data = {
    "username": "octocat",
    "email": "octocat@github.com",
    "name": "Octo Cat"
}

json_data = requests.json(data)

print(json_data)

The code above creates a Python dictionary containing information about a user, and then uses the json method to convert it into a JSON-formatted string. The resulting string is then printed to the console.

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-12-10 What is the default value for enum variable?
2021-12-10 StoreName Enum
2021-12-10 X509Certificate2 Class
2021-12-10 Create a .pfx/.p12 Certificate File Using OpenSSL
2021-12-10 How Certificate Chains Work 证书链如何工作的
2021-12-10 OpenSSL and s_client - why is a private key required from the client?
2021-12-10 How to validate a client certificate 客户端证书
点击右上角即可分享
微信分享提示