Home >Backend Development >Python Tutorial >How to call requests package in python

How to call requests package in python

silencement
silencementOriginal
2019-06-13 09:49:183338browse

How to call requests package 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

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")

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)

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

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

Statement:
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