requests Module of Python Full Stack Road Standard Library Series

Keywords: Python JSON github git

Install Requests module

Requests module is officially installed in two ways:

pip Installation

pip install requests

Source mode installation

git clone git://github.com/kennethreitz/requests.git
cd requests
python setup.py install

Verify that the installation was successful

As explained by python, try importing the module. If the import succeeds, the installation succeeds. Otherwise, you need to check the execution error there.

What I don't know in the process of learning can be added to me?
python learning communication deduction qun, 784758214
 There are good learning video tutorials, development tools and e-books in the group.
Share with you the current talent needs of python enterprises and how to learn python from zero foundation, and what to learn
C:\Users\anshengme> python
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2016, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests

Environmental preparation

Install gunicorn and httpbin

sudo pip3 install gunicorn httpbin

Start a gunicornServer

⇒  sudo gunicorn httpbin:app
[2016-10-27 11:45:08 +0800] [12175] [INFO] Starting gunicorn 19.6.0
[2016-10-27 11:45:08 +0800] [12175] [INFO] Listening at: https://blog.ansheng.me:8000 (12175)
[2016-10-27 11:45:08 +0800] [12175] [INFO] Using worker: sync
[2016-10-27 11:45:08 +0800] [12178] [INFO] Booting worker with pid: 12178

Opening the browser and entering ``` will give you the following page, which is equivalent to starting an http server locally to facilitate learning requests module

A simple request applet

The following small program will request the URI link'https://blog.ansheng.me:8000/ip'to get the corresponding data.

#!/use/bin/env python3
# _*_ coding:utf-8 _*_
import requests

URL_IP = 'https://blog.ansheng.me:8000/ip'

def use_params_requests():
    # parameter
    params = {'params1': 'Hello', 'params2': 'World'}
    # Send requests
    response = requests.get(URL_IP, params=params)
    print("Response status code:", response.status_code, response.reason)
    print("Head returned:", response.headers)
    print("Convert the returned data to json:", response.json())
    print("The text of the response:", response.text)

if __name__ == '__main__':
    use_params_requests()

Send requests

Getting User Information through API s Provided by GITHUB

#!/use/bin/env python3
# _*_ coding:utf-8 _*_
import requests
import json

URL = 'https://api.github.com'

def build_uri(endpoint):
    return '/'.join([URL, endpoint])

def better_print(json_str):
    return json.dumps(json.loads(json_str), indent=4)

def request_method():
    # Get all the information of the user
    response = requests.get(build_uri('users/anshengme'))
    print(better_print((response.text)))

    print("\n")

    # Get the user's mailbox
    response = requests.get(build_uri('user/emails'), auth=("anshengme.com@gmail.com", "xxxxxx"))
    print(better_print((response.text)))

if __name__ == '__main__':
    request_method()

Requests with parameters

# Using params to transmit data
def params_request():
    response = requests.get(build_uri('users'), params={'since': 11})
    print(better_print(response.text))
    print(response.request.headers)
    print(response.url)

# Using json method
def json_request():
    response = requests.get(build_uri('user'), auth=("username", "email"), json={"name": "asdas"})
    print(better_print(response.text))

exception handling

def timeout_request():
    try:
        response = requests.get(build_uri('user/emails'), timeout=10)
        response.raise_for_status()
    except Exception as e:
        print(e)
    else:
        print(response.text)
        print(response.status_code)

Custom request

from requests import Request, Session

def hard_request():
    s = Session()
    # Create a request
    headers = {'User-Agent': 'fake1.3.4'}
    req = Request('GET', build_uri('user/emails'), auth=('anshengme.com@gmail.com', 'xxxxxx'), headers=headers)
    prepped = req.prepare()
    print("Request Head", prepped.headers)
    # Send requests
    resp = s.send(prepped)
    print(resp.status_code)
    print(resp.request.headers)
    print(resp.text)

Example

Download pictures/files

#!/use/bin/env python3
# _*_ coding:utf-8 _*_
import requests
from contextlib import closing

# The Model of Stream Transport
def download_img():
    url = "http://www.sinaimg.cn/IT/cr/2016/0331/725124517.jpg"
    # headers = {
    #     'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'}
    # response = requests.get(url, headers=headers, stream=True)
    response = requests.get(url, stream=True)
    print(response.status_code, response.reason)

    with open('github.jpg', 'wb') as fd:
        for chunk in response.iter_content(128):
            fd.write(chunk)

def download_img_improved():
    url = "http://www.sinaimg.cn/IT/cr/2016/0331/725124517.jpg"
    with closing(requests.get(url, stream=True)) as response:
        # Open and write files
        with open('github1.jpg', 'wb') as fd:
            for chunk in response.iter_content(128):
                fd.write(chunk)

download_img()
download_img_improved()

If you are still confused in the world of programming, you can join our Python learning button qun: 784758214 to see how our predecessors learned! Exchange experience! I am a senior Python development engineer, from basic Python script to web development, crawler, django, data mining and so on, zero-based to the actual project data have been collated. To every Python buddy! Share some learning methods and small details that need attention. Click to join us. python learner gathering place

Event hook for handling response

#!/use/bin/env python3
# _*_ coding:utf-8 _*_
import requests

def get_key_info(response, *args, **kwargs):
    print(response.headers["Content-Type"])

def main():
    requests.get("https://www.baidu.com", hooks=dict(response=get_key_info))

if __name__ == "__main__":
    main()

Posted by oc1000 on Sun, 18 Aug 2019 23:09:08 -0700