Basic of python requests library basic

The official document guidelines are more cumbersome, so this article condenses the necessary content.

pip install requests:

pip  install  requests

Import package:

>>> import requests

get or post request:

# Get request without optional parameters
>>> r = requests.get(url='https://github.com/timeline.json')
# Post request without optional parameters
>>> r = requests.post(url="http://httpbin.org/post")

Other requests (pass by directly, rarely used, and not much in this tutorial)

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

get\post parameter description:

HTTP request get and post are the most commonly used, URL is a required parameter, commonly used parameters are params, data, JSON, files, timeout, headers, cookies; other basic unused ones are verified, cert, auth, allow_redirects, proxies, Hooks, stream.

get request text and binary data content

The get request is the simplest, and the amount of data sent is relatively small.

Example: Request with multiple parameters, return text data like paper

import requests

# GET request with parameters, timeout request timeout
params = {'key1':'python','key2':'java'}
r = requests.get(url='http://httpbin.org/get', params=params, timeout=3)

# Pay attention to the URL address, it has spliced the parameters together
print('URL address:', r.url)
# Response status code, successful return 200, failure 40x or 50x
print('Request status code:', r.status_code)
print('header information:', r.headers)
print('cookie information:', r.cookies)
print('Response data:', r.text)
# If the response is json data, you can use r.json() to automatically convert to dict
print('Response to json data', r.json())

get returns binary data, such as pictures.

from PIL import Image
from io import BytesIO
import requests

# Request to get the picture and save
r = requests.get('######.jpg')
i = Image.open(BytesIO(r.content))
# i.show() # View picture
# Save the picture
with open('img.jpg','wb') as fd:
    for chunk in r.iter_content():
        fd.write(chunk)

Note: PIL cannot be used, please install pip install pillow

post request, upload form, text, file\picture
Post request is more complicated than get, and the requested data has a variety of forms (form-data), text (json\xml, etc.), file stream (picture\file), etc.

For a post request submitted in the form of a form, only the data needs to be passed to the data parameter of the post() method.

For post requests submitted in the form of JSON text, one way is to dump the JSON data and pass it to the data parameter, and the other way is to pass the JSON data directly to the JSON parameter of the post() method.

For a post request submitted by a single file, stream the file to the files parameter of the post() method.

For post requests submitted by multiple files, set the files to a list of tuples, where the tuple structure is (form_field_name, file_info); then pass the data to the files of the post() method.

post form request:

import requests, json

# With parameter form type post request
data={'custname':'woodman','custtel':'###','#####':'##@##.com',
      'size':'small'}
r = requests.post('######', data=data)
print('Response data:', r.text)

post JSON request:

# json data request
url ='https://api.github.com/some/endpoint'
payload = {'some':'data'}
# You can use json.dumps(dict) to compile the code
r = requests.post(url, data=json.dumps(payload))
print('Response data:', r.text)

# You can directly use json parameters to pass json data
r = requests.post(url, json=payload)
print('Response data:', r.text)

in progress…

Leave a Comment

Your email address will not be published.

Scroll to Top