Home Backend Development Python Tutorial Detailed explanation of advanced usage of Requests library

Detailed explanation of advanced usage of Requests library

May 09, 2017 pm 03:14 PM
python

Although the urllib2 module in Python's standard library already contains most of the functions we usually use, its API is really uncomfortable to use. It is no longer suitable for the current era and the modern Internet. The birth of Requests gives us a better choice. This article introduces the advanced usage of the third-party library Requests library in Python.

1. Installation of Requests library

#Use pip to install, if you install the pip package (a Python Package management tool, I don’t know if you can use Baidu), or an integrated environment, such as Python (x, y) or anaconda, you can directly use pip to install the Python library.

$ pip install requests
Copy after login

After the installation is completed, let’s take a look at the basic method:

#get请求方法
 >>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
#打印get请求的状态码
 >>> r.status_code
200
#查看请求的数据类型,可以看到是json格式,utf-8编码
 >>> r.headers['content-type']
'application/json; charset=utf8'
 >>> r.encoding
'utf-8'
#打印请求到的内容
 >>> r.text
u'{"type":"User"...'
#输出json格式数据
 >>> r.json()
 {u'private_gists': 419, u'total_private_repos': 77, ...}
Copy after login

Let’s take a look at a small chestnut:

#小例子
import requests

r = requests.get('http://www.baidu.com')
print type(r)
print r.status_code
print r.encoding
print r.text
print r.cookies
'''请求了百度的网址,然后打印出了返回结果的类型,状态码,编码方式,Cookies等内容 输出:'''
<class &#39;requests.models.Response&#39;>
200
UTF-8
<RequestsCookieJar[]>
Copy after login

2. http basic request

# The requests library provides all basic request methods of http. For example:

r = requests.post("http://httpbin.org/post")
r = requests.put("http://httpbin.org/put")
r = requests.delete("http://httpbin.org/delete")
r = requests.head("http://httpbin.org/get")
r = requests.options(http://httpbin.org/get)
Copy after login

Basic GET request

r = requests.get("http://httpbin.org/get")
#如果想要加参数,可以利用 params 参数:
import requests
payload = {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}
r = requests.get("http://httpbin.org/get", params=payload)
print r.url

#输出:http://httpbin.org/get?key2=value2&key1=value1
Copy after login

If you want to request a JSON file, you can use the json() method to parse it. For example, write a JSON file yourself and name it a.json , the content is as follows:

["foo", "bar", {
"foo": "bar"
}]
#利用如下程序请求并解析:
import requests
r = requests.get("a.json")
print r.text
print r.json()
&#39;&#39;&#39;运行结果如下,其中一个是直接输出内容,另外一个方法是利用 json() 方法 解析,感受下它们的不同:&#39;&#39;&#39;
["foo", "bar", {
"foo": "bar"
}]
[u&#39;foo&#39;, u&#39;bar&#39;, {u&#39;foo&#39;: u&#39;bar&#39;}]
Copy after login

If you want to get the raw socket response from the server, you can get r.raw. However, stream=True needs to be set in the initial request.

r = requests.get(&#39;https://github.com/timeline.json&#39;, stream=True)
r.raw
#输出
<requests.packages.urllib3.response.HTTPResponse object at 0x101194810>
r.raw.read(10)
&#39;\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03&#39;
Copy after login

In this way, the original socket content of the web page is obtained.

If you want to add headers, you can pass headers parameters:

import requests

payload = {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}
headers = {&#39;content-type&#39;: &#39;application/json&#39;}
r = requests.get("http://httpbin.org/get", params=payload, headers=headers)
print r.url
#通过headers参数可以增加请求头中的headers信息
Copy after login

3. Basic POST request

For For POST requests, we generally need to add some parameters to it. Then the most basic parameter passing method can use the data parameter.

import requests

payload = {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}
r = requests.post("http://httpbin.org/post", data=payload)
print r.text
#运行结果如下:
{
"args": {}, 
"data": "", 
"files": {}, 
"form": {
"key1": "value1", 
"key2": "value2"
}, 
"headers": {
"Accept": "*/*", 
"Accept-Encoding": "gzip, deflate", 
"Content-Length": "23", 
"Content-Type": "application/x-www-form-urlencoded", 
"Host": "http://httpbin.org", 
"User-Agent": "python-requests/2.9.1"
}, 
"json": null, 
"url": "http://httpbin.org/post"
}
Copy after login

You can see that the parameters were passed successfully, and then the server returned the data we passed.

Sometimes the information we need to send is not in the form of a form. We need to send data in JSON format, so we can use the json.dumps() method to serialize the form data.

import json
import requests

url = &#39;http://httpbin.org/post&#39;
payload = {&#39;some&#39;: &#39;data&#39;}
r = requests.post(url, data=json.dumps(payload))
print r.text

#运行结果:
{
"args": {}, 
"data": "{\"some\": \"data\"}", 
"files": {}, 
"form": {}, 
"headers": {
"Accept": "*/*", 
"Accept-Encoding": "gzip, deflate", 
"Content-Length": "16", 
"Host": "http://httpbin.org", 
"User-Agent": "python-requests/2.9.1"
}, 
"json": {
"some": "data"
}, 
"url": "http://httpbin.org/post"
}
Copy after login

Through the above method, we can POST data in JSON format

If you want to upload a file, just use the file parameter directly:

#新建一个 test.txt 的文件,内容写上 Hello World!
import requests

url = &#39;http://httpbin.org/post&#39;
files = {&#39;file&#39;: open(&#39;test.txt&#39;, &#39;rb&#39;)}
r = requests.post(url, files=files)
print r.text

{
"args": {}, 
"data": "", 
"files": {
"file": "Hello World!"
}, 
"form": {}, 
"headers": {
"Accept": "*/*", 
"Accept-Encoding": "gzip, deflate", 
"Content-Length": "156", 
"Content-Type": "multipart/form-data; boundary=7d8eb5ff99a04c11bb3e862ce78d7000", 
"Host": "http://httpbin.org", 
"User-Agent": "python-requests/2.9.1"
}, 
"json": null, 
"url": "http://httpbin.org/post"
}
Copy after login

In this way we will complete it successfully uploaded a file.

requests supports streaming uploads, which allows you to send large data streams or files without reading them into memory first. To use streaming upload, you only need to provide a class file object for your request body, which is very convenient:

with open(&#39;massive-body&#39;) as f:
requests.post(&#39;http://some.url/streamed&#39;, data=f)
Copy after login

4. Cookies

If a response contains cookies, then we can use cookies Variables to get:

import requests

url = &#39;Example Domain&#39;
r = requests.get(url)
print r.cookies
print r.cookies[&#39;example_cookie_name&#39;]
Copy after login

The above program is only a sample, you can Use cookies variables to get site cookies

In addition, you can use cookies variables to send cookie information to the server:

import requests

url = &#39;http://httpbin.org/cookies&#39;
cookies = dict(cookies_are=&#39;working&#39;)
r = requests.get(url, cookies=cookies)
print r.text
#输出:
&#39;{"cookies": {"cookies_are": "working"}}&#39;
Copy after login

5. Timeout configuration

You can use the timeout variable to configure the maximum request time

requests.get(‘Build software better, together&#39;, timeout=0.001)
Copy after login

Note: timeout is only valid for the connection process and is related to the download of the response body Nothing to do.

In other words, this time only limits the requested time. Even if the returned response contains a large amount of content, it will take some time to download.

6. Session object

In the above requests, each request is actually equivalent to initiating a new request. . This is equivalent to the effect of using a different browser to open each request separately. That is, it does not refer to a session, even if the same URL is requested. For example:

import requests
requests.get(&#39;http://httpbin.org/cookies/set/sessioncookie/123456789&#39;)
r = requests.get("http://httpbin.org/cookies")
print(r.text)
#结果是:
{
"cookies": {}
}
Copy after login

Obviously, this is not in a session and cookies cannot be obtained. So what should we do if we need to maintain a persistent session on some sites? Just like using a browser to browse Taobao, jumping between different tabs actually creates a long-term session.

The solution is as follows:

import requests

s = requests.Session()
s.get(&#39;http://httpbin.org/cookies/set/sessioncookie/123456789&#39;)
r = s.get("http://httpbin.org/cookies")
print(r.text)
#在这里我们请求了两次,一次是设置 cookies,一次是获得 cookies
{
"cookies": {
"sessioncookie": "123456789"
}
}
Copy after login

It is found that cookies can be successfully obtained. This is to establish a session.

So since the session is a global variable, we can definitely use it for global configuration.

import requests

s = requests.Session()
s.headers.update({&#39;x-test&#39;: &#39;true&#39;})
r = s.get(&#39;http://httpbin.org/headers&#39;, headers={&#39;x-test2&#39;: &#39;true&#39;})
print r.text
&#39;&#39;&#39;通过 s.headers.update 方法设置了 headers 的变量。然后我们又在请求中 设置了一个 headers,那么会出现什么结果?很简单,两个变量都传送过去了。 运行结果:&#39;&#39;&#39;
{
"headers": {
"Accept": "*/*", 
"Accept-Encoding": "gzip, deflate", 
"Host": "http://httpbin.org", 
"User-Agent": "python-requests/2.9.1", 
"X-Test": "true", 
"X-Test2": "true"
}
}
Copy after login

What if the headers passed by the get method are also x-test?

r = s.get(&#39;http://httpbin.org/headers&#39;, headers={&#39;x-test&#39;: &#39;true&#39;})

#它会覆盖掉全局的配置:
{
"headers": {
"Accept": "*/*", 
"Accept-Encoding": "gzip, deflate", 
"Host": "http://httpbin.org", 
"User-Agent": "python-requests/2.9.1", 
"X-Test": "true"
}
}
Copy after login

What if you don’t want a variable in the global configuration? It's easy, just set it to None.

r = s.get(&#39;http://httpbin.org/headers&#39;, headers={&#39;x-test&#39;: None})
{
"headers": {
"Accept": "*/*", 
"Accept-Encoding": "gzip, deflate", 
"Host": "http://httpbin.org", 
"User-Agent": "python-requests/2.9.1"
}
}
Copy after login

The above is the basic usage of session session.

7. SSL Certificate Verification

Now you can see websites starting with https everywhere. Requests can verify SSL certificates for HTTPS requests. Like a web browser. To check the SSL certificate of a certain host, you can use the verify parameter, because the 12306 certificate was not invalid some time ago. Let’s test it:

import requests

r = requests.get(&#39;https://kyfw.12306.cn/otn/&#39;, verify=True)
print r.text
#结果:
requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)
Copy after login

Let’s try github’s :

import requests

r = requests.get(&#39;Build software better, together&#39;, verify=True)
print r.text
Copy after login

Well, normal request, because there is too much content, I won’t paste the output.

如果我们想跳过刚才 12306 的证书验证,把 verify 设置为 False 即可:

import requests

r = requests.get(&#39;https://kyfw.12306.cn/otn/&#39;, verify=False)
print r.text
Copy after login

发现就可以正常请求了。在默认情况下 verify 是 True,所以如果需要的话,需要手动设置下这个变量。

八、代理

如果需要使用代理,你可以通过为任意请求方法提供 proxies 参数来配置单个请求。

import requests

proxies = {
"https": "http://41.118.132.69:4433"
}
r = requests.post("http://httpbin.org/post", proxies=proxies)
print r.text
#也可以通过环境变量 HTTP_PROXY 和 HTTPS_PROXY 来配置代理
export HTTP_PROXY="http://10.10.1.10:3128"
export HTTPS_PROXY=http://10.10.1.10:1080
Copy after login

总结

【相关推荐】

1. Python免费视频教程

2. Python基础入门教程

3. Python遇见数据采集视频教程

The above is the detailed content of Detailed explanation of advanced usage of Requests library. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

See all articles