Requs Library - Web Crawler

Introduction to requests Library
Official documents: requests quick start
It is written in great detail and recommended to read the official documents.

Get started quickly

Import requests Library

import requests

Send request:

r = requests.get('url')
r ="")
r = requests.put("")
r = requests.delete("")
r = requests.head("")
r = requests.options("")

The above are http request types, which are commonly used for get and post requests.
post is generally used to send data to the other party to get content.

Pass URL parameter
You may often want to pass some data to the query string of a URL.If you build a URL manually, the data is placed in the URL as key/value pairs, followed by a question mark.For example,
Requests allow you to use params keyword parameters to provide them in a dictionary.For example, if you want to pass key1=value1 and key2=value2 to, you can use the following code:

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("", params=payload)
By printing out the URL, you can see that the URL is correctly encoded:

Note that keys with a value of None in the dictionary will not be added to the query string of the URL.

You can also pass in a list as a value:

>>>payload = {'key1': 'value1', 'key2': ['value2', 'value3']}

>>> r = requests.get('', params=payload)
>>> print(r.url)

Customize Headers
When crawling a network resource, it is generally easy for the server to identify the crawler and then deny access.At this point, we usually want the crawler to simulate the browser and customize the headers.

url = ''
headers = {'user-agent': 'my-app/0.0.1'}

r = requests.get(url, headers=headers)

Response Content

import requests
r = requests.get('')

#Or use.content to access the request response body in bytes
#Requests automatically decode gzip and deflate the encoded response data for you.

Requests automatically decode content from the server.Most unicode character sets can be seamlessly decoded."
After the request is sent, Requests makes a valid guess about the encoding of the response based on the HTTP header.When you access r.text, Requests uses its guessed text encoding.You can find out what encoding Requests uses, and you can use the r.encoding attribute to change it:

>>>r.encoding = 'ISO-8859-1'

post method
Now many websites have to be logged in to crawl, at this time we have to pass some data to the server to get the content.
To do this, simply pass a dictionary to the data parameter.Your data dictionary is automatically encoded as a form when requested:

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

>>> r ="", data=payload)
>>> print(r.text)
  "form": {
    "key2": "value2",
    "key1": "value1"

Session session
Session objects allow you to keep certain parameters across requests.It also keeps cookie s between all requests made by the same Session instance, using the connection pooling feature of urllib3.

s = requests.Session()

r = s.get("")

# '{"cookies": {"sessioncookie": "123456789"}}'

Response Status Code
We can detect the response status code:

>>> r = requests.get('')
>>> r.status_code
>>> r.status_code == status code query object

An error request (a 4XX client error, or a 5XX server error response) was sent and we can throw an exception via Response.raise_for_status():

>>> bad_r = requests.get('')
>>> bad_r.status_code

>>> bad_r.raise_for_status()
Traceback (most recent call last):
  File "requests/", line 832, in raise_for_status
    raise http_error
requests.exceptions.HTTPError: 404 Client Error

Response Header

>>> r.headers
    'content-encoding': 'gzip',
    'transfer-encoding': 'chunked',
    'connection': 'close',
    'server': 'nginx/1.0.4',
    'x-runtime': '148ms',
    'etag': '"e1ca502697e5c9317743dc078f67693f"',
    'content-type': 'application/json'


#If a response contains cookie s, you can quickly access them:
>>>url = ''
>>> r = requests.get(url)

>>> r.cookies['example_cookie_name']

#To send your cookies to the server, use the cookies parameter:
>>> url = ''
>>> cookies = dict(cookies_are='working')
>>> r = requests.get(url, cookies=cookies)
>>> r.text
'{"cookies": {"cookies_are": "working"}}'

If you want to save a cookie, you usually use the cookielib library.

import requests
import cookielib
#Set cookie s
session = requests.Session()
cookies = 'temp/cookie.txt'
#filename is not only a file, it can also be a str object
session.cookies = cookielib.LWPCookieJar(filename=cookies)
#Log in again to load cookie s directly without entering an account password
r =session.cookies.load()

#After getting cookie s


>>>requests.get('', timeout=0.001)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
requests.exceptions.Timeout: HTTPConnectionPool(host='', port=80): Request timed out. (timeout=0.001)

If you need to use a proxy, you can configure a single request by providing a proxies parameter for any request method:

import requests

proxies = {
  "http": "",
  "https": "",

requests.get("", proxies=proxies)

You can also configure the proxy through environment variables HTTP_PROXY and HTTPS_PROXY.

$ export HTTP_PROXY=""
$ export HTTPS_PROXY=""

$ python
>>> import requests
>>> requests.get("")

To set up a proxy for a particular connection mode or host, use scheme://hostname as the key, which matches the specified host and connection mode.

proxies = {'': ''}

In addition to the basic HTTP proxy, Request also supports proxies for the SOCKS protocol.This is an optional feature and you need to install third-party libraries to use it.
You can use pip to get dependencies

$ pip install requests[socks]


proxies = {
    'http': 'socks5://user:pass@host:port',
    'https': 'socks5://user:pass@host:port'

Comprehensive example

import requests
url = '' 

headers =  {
        'Accept': '*/*',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'X-Requested-With': 'XMLHttpRequest',
        'Referer': '',
        'Accept-Language': 'en-GB,en;q=0.8,zh-CN;q=0.6,zh;q=0.4',
        'Accept-Encoding': 'gzip, deflate, br',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36',
        'Host': ''

proxies = {
    "https": "",
    "https": ""

r = requests.get(url,timeout=10, proxies=proxies, headers= headers)

post_data = {
            '_xsrf': _xsrf,
            self.account_name: username,
            'remember_me': 'true',
r1 =,data=self.post_data,timeout=10, proxies=proxies, headers= headers).content.decode('utf8)

s = requests.Session()
r = s.get("")
# '{"cookies": {"sessioncookie": "123456789"}}'

Tags: Session encoding github JSON

Posted on Wed, 10 Jul 2019 12:40:13 -0400 by dmikester1