Home > Backend Development > Python Tutorial > How to import requests in python

How to import requests in python

silencement
Release: 2019-07-08 10:24:21
Original
4116 people have browsed it

How to import requests in python

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 Requests Can the module be detached? 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.

Import requests

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

import requests
Copy after login

Request url

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

Send a get request without parameters:

 r=requests.get("http://pythontab.com/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, so what if the request requires parameters?

Send a get request with parameters

payload = {'key1': 'value1', 'key2': 'value2'}

r = requests.get("http://pythontab.com/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 of the request to see if it is correct:

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

The above is the detailed content of How to import requests in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template