Table of Contents
1. Import
2. Request URL
1. Send a get request without parameters:
2. Send a get request with parameters
3. Send a post request
3. Obtain the return information
4. About headers
5. About Cookies
6. About redirection
7. About the request time
8 , About proxy
九、关于session
十、下载页面
Home Backend Development Python Tutorial Detailed explanation of how to use Python's Requests module

Detailed explanation of how to use Python's Requests module

Aug 16, 2017 pm 01:20 PM
python requests Example

The Requests module is a module used for network access. In fact, there are many similar modules, such as urllib, urllib2, httplib, httplib2. They basically provide similar functions, so why can the Requests module stand out? You can open its official website and take a look. It is an http module for "human beings". So, how human is it? I believe that if you have used modules such as urllib before, you will find that it is indeed very user-friendly.

1. Import

After the download is completed, importing the module is very simple. The code is as follows:

import requests
Copy after login

2. Request URL

Here we list the most common The syntax for sending get or post requests.

1. Send a get request without parameters:

r=requests.get("http://php.cn/justTest")
Copy after login

Now, we get a response object r, we can use this object to get any information we want.

In the above example, the get request does not have any parameters. What if the request requires parameters?

2. Send a get request with parameters

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://php.cn/justTest", params=payload)
Copy after login

As we know from the above, our get parameters are passed as params keyword parameters.

We can print the specific URL requested to see if it is correct:

>>>print r.url
http://pythontab.com/justTest?key2=value2&key1=value1
Copy after login

You can see that the correct URL was indeed accessed.

You can also pass a list to a request parameter:

>>> payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
>>> r = requests.get("http://php.cn/justTest", params=payload)
>>> print r.url
http://pythontab.com/justTest?key1=value1&key2=value2&key2=value3
Copy after login

The above is the basic form of the get request.

3. Send a post request

r = requests.post("http://php.cn/postTest", data = {"key":"value"})
Copy after login

As we know from the above, the post request parameters are passed as data keyword parameters.

The current data parameter passes a dictionary, we can also pass a json format data, as follows:

>>> import json
>>> import requests
>>> payload = {"key":"value"}
>>> r = requests.post("http://php.cn/postTest", data = json.dumps(payload))
Copy after login

Since sending json format data is too common, in the higher version of the Requests module , the keyword parameter json has been added. You can directly send json data to the post request without using the json module. See below:

>>> payload = {"key":"value"}
>>> r = requests.post("http://php.cn/postTest", json=payload)
Copy after login

What if we want to post a file? At this time, you need to use the files parameter:

>>> url = 'http://php.cn/postTest'
>>> files = {'file': open('report.xls', 'rb')}
>>> r = requests.post(url, files=files)
>>> r.text
Copy after login

We can also specify additional information such as the file name when posting the file:

>>> url = 'http://php.cn/postTest'
>>> files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
>>> r = requests.post(url, files=files)
Copy after login

tips: It is strongly recommended to use binary mode to open the file, because If opened in text file format, an error may occur due to the "Content-Length" header.

As you can see, it is easy to send requests using Requests!

3. Obtain the return information

Let’s take a look at how to obtain the return information after sending the request. Let’s continue to use the top example: In what encoding format is

>>> import requests
>>> r=requests.get('http://php.cn/justTest')
>>> r.text
Copy after login

r.text output?

>>> r.encoding
'utf-8'
Copy after login

It turned out to be output in utf-8 format. What if I want to change the output format of r.text?

>>> r.encoding = 'ISO-8859-1'
Copy after login

This changes the output format to "ISO-8859-1".

There is also an output statement called r.content, so what is the difference between this and r.text? r.content returns a byte stream, which can be used if we request an image address and want to save the image. Here is a code snippet as follows:

def saveImage( imgUrl,imgName ="default.jpg" ):
    r = requests.get(imgUrl, stream=True)
    image = r.content
    destDir="D:\"
    print("保存图片"+destDir+imgName+"\n")
    try:
        with open(destDir+imgName ,"wb") as jpg:
            jpg.write(image)     
            return
    except IOError:
        print("IO Error")
        return
    finally:
        jpg.close
Copy after login

The r.text just introduced returns String, then, if the response corresponding to the request is a json, can I directly get the data in json format? r.json() is prepared for this.

We can also get the original data returned by the server, just use r.raw.read(). However, if you really want to get the original return data, remember to add the "stream=True" option when requesting, such as:

r = requests.get('https://api.github.com/events', stream=True)。
Copy after login

We can also get the response status code:

>>> r = requests.get('http://php.cn/justTest')
>>> r.status_code
200
Copy after login

You can also use requests.codes.ok to refer to the return value of 200:

>>> r.status_code == requests.codes.ok
True
Copy after login

4. About headers

We can print out the response header:

>>> r= requests.get("http://php.cn/justTest")
>>> r.headers
Copy after login

`r .headers` returns a dictionary, for example:

{
    'content-encoding': 'gzip',
    'transfer-encoding': 'chunked',
    'connection': 'close',
    'server': 'nginx/1.0.4',
    'x-runtime': '147ms',
    'etag': '"e1ca502697e5c9317743dc078f67693a"',
    'content-type': 'application/json'
}
Copy after login

We can use the following method to obtain part of the response headers for judgment:

r.headers['Content-Type']
Copy after login

or

r.headers.get('Content-Type')
Copy after login

If we want What should we do to get the request header (that is, the header information we send to the server)? Can be obtained directly using r.request.headers.

At the same time, we can also add custom headers (passed through headers keyword parameters) when requesting data:

>>> headers = {'user-agent': 'myagent'}
>>> r= requests.get("http://php.cn/justTest",headers=headers)
Copy after login

5. About Cookies

If a response If cookies are included, we can use the following method to get them:

>>> url = 'http://www.php.cn'
>>> r = requests.get(url)
>>> r.cookies['example_cookie_name']
'example_cookie_value'
Copy after login

We can also send our own cookies (using the cookies keyword parameters):

>>> url = 'http://php.cn/cookies'
>>> cookies={'cookies_are':'working'}
>>> r = requests.get(url, cookies=cookies)
Copy after login

6. About redirection

Sometimes when we request the URL, the server will automatically redirect our request. For example, github will redirect our http request to https request. We can use r.history to view redirects:

>>> r = requests.get('http://php.cn/')
>>> r.url
'http://php.cn/'
>>> r.history
[]
Copy after login

As you can see from the above example, we use the http protocol to access, but in r.url, the https protocol is printed. So what should I do if I insist that the server use the http protocol, which means that the server is prohibited from automatically redirecting? Use the allow_redirects parameter:

r = requests.get('http://php.cn', allow_redirects=False)
Copy after login

7. About the request time

We can use the timeout parameter to set the request timeout of the url (time unit is seconds):

requests.get('http://php.cn', timeout=1)
Copy after login

8 , About proxy

We can also specify a proxy in the program for http or https access (using proxies keyword parameters), as follows:

proxies = {
  "http": "http://10.10.1.10:3128",
  "https": "http://10.10.1.10:1080",
}
requests.get("http://php.cn", proxies=proxies)
Copy after login

九、关于session

我们有时候会有这样的情况,我们需要登录某个网站,然后才能请求相关url,这时就可以用到session了,我们可以先使用网站的登录api进行登录,然后得到session,最后就可以用这个session来请求其他url了:

s=requests.Session()
login_data={'form_email':'youremail@example.com','form_password':'yourpassword'}
s.post("http://pythontab.com/testLogin",login_data)
r = s.get('http://pythontab.com/notification/')
print r.text
Copy after login

其中,form_email和form_password是豆瓣登录框的相应元素的name值。

十、下载页面

使用Requests模块也可以下载网页,代码如下:

r=requests.get("http://www.php.cn")
with open("haha.html","wb") as html:
    html.write(r.content)
html.close()
Copy after login

The above is the detailed content of Detailed explanation of how to use Python's Requests module. 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.

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.

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 sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

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.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

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.

See all articles