首页 > 编程语言 >Python requests

Python requests

时间:2022-12-10 09:11:50浏览次数:69  
标签:Python page content json requests data method

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": "[email protected]",
    "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.

 

标签:Python,page,content,json,requests,data,method
From: https://www.cnblogs.com/chucklu/p/16970755.html

相关文章