1 request module

An official document is really easy to use

The official document: https://2.python-requests.org//zh_CN/latest/index.html

参考blog: https://www.cnblogs.com/humiao-0626/category/1465538.html

1. Quick Start

# -*- coding:utf-8 -*-
"""
requests模块
https://2.python-requests.org//zh_CN/latest/index.html
"""

import requests#

A transmission request

r = requests.get("https://www.cnblogs.com/")  #  r: Response 对象
print(r)

# r = requests.post("http://httpbin.org/post",data={'key':'value'})
# r = requests.put("http://httpbin.org/put",data={'key':'value'})
# r = requests.delete("http://httpbin.org/delete")
# r = requests.head("http://httpbin.org/get")
# r = requests.options("http://httpbin.org/get")

Url parameter passed 2

# payload = {"key1":"value1","key2":"value2"}
payload = {"key1":"value1","key2":["value2","value3"]}

r = requests.get("http://httpbin.org/get",params=payload)

#print(r.url) # http://httpbin.org/get?key1=value1&key2=value2
print(r.url) # http://httpbin.org/get?key1=value1&key2=value2&key2=value3

3 response content

requests.get = R & lt ( "https://api.github.com/events") 
Print (r.text)


# Response coding type
Print (r.encoding) #. 8 UTF-

Print (r.content) # B '[ { "the above mentioned id": "10,124,980,852", "of the type": "PushEvent", "the Actor": { "the above mentioned id": 387
#, such as HTTP and XML itself can specify the encoding.
# In this case, you should use r.content to find coding,
# r.encoding then set the corresponding code. This
# will be able to use the right kind of code resolution r.text up.

4 binary response content

Print (r.content) # # B '[{ "ID": "10,124,980,852", "type": "PushEvent", "the Actor": { "ID": 387 
# Requests for you automatically transmitting the encoded decoding gzip and deflate the response data.

# To request the return of binary data to create a picture
'' '
from PIL Import Image
from IO Import BytesIO
i = Image.open (BytesIO (r.content))
' ''

5 JSON response content

requests.get = R & lt ( 'https://api.github.com/events') 
r.json () # B' [{ "ID": "10,125,000,497", "type": "PushEvent", "the Actor": { "the above mentioned id": 28,391,787, "the Login": "m1ha5", "display_login": "M1"}}]

# content of the response is a 401 (Unauthorized), tries to access r.json () will throw ValueError: No JSON object could be decoded exception.
# To check whether the request is successful, please use r.raise_for_status () or check whether r.status_code and you expect the same
# Print (r.raise_for_status ())
Print (r.status_code)

6 in response to the original content

    # Want to obtain the original response from the server socket 
# ensure stream = True is set in the initial request
R & lt requests.get = ( 'https://api.github.com/events', stream = True)
Print (R & lt. RAW) # <Object urllib3.response.HTTPResponse AT 0x000001D3FD5525C0>

# Print (r.raw.read (10)) B # '\ X1F \ x8b \ X08 \ xOO \ xOO \ xOO \ xOO \ xOO \ xOO \ X03'
# the following text stream mode saved to a file
with Open ( 'test.txt', 'WB') FD AS:
for the chunk in r.iter_content (chunk_size = 10):
fd.write (the chunk)

7 custom request headers

= URL "https://api.github.com/some/endpoint" 
headers = { 'User-Agent': 'My-App / 0.0.1'}
R & lt requests.get = (URL, headers = headers)
# Custom header lower priority than specific information source
# Note: All values must be a header string, bytestring or unicode.

8 more complex POST requests

= URL 'http://httpbin.org/post' 
# payload = { 'key1': 'vaule1', 'key2': 'value2'}

# pass a tuple list for the data parameter. When a plurality of elements in the form using the same key, this approach is particularly effective:
payload = (( 'key1', 'VALUE1'), ( 'key1', 'VALUE1'))

R & lt requests.post = (URL URL = , data = payload)
Print (r.text) # returns the response content


data is not transmitted encoded form in the form of # you pass a String
# e.g., Github API v3 receiving the encoded as JSON POST / PATCH data:
Import JSON
URL = ' https://api.github.com/some/endpoint '
payload = {' some ':' Data '}

# dict themselves to encode
# r = requests.post (url = url , data = json.dumps (payload) )

# json transmitted directly by the parameter
R & lt requests.post = (URL = URL, json = payload)

Print (r.text)

9 POST encoding a multi-part (Multipart-Encoded) file

Official Documents

10 the response status code

= url "http://httpbin.org/get" 
r = requests.get (url = url)
Print (r.status_code)
Print (r.status_code == requests.codes.ok) # # True Requests also comes with a built-in query object status code

# if sent a bad request (a client error 4XX, 5XX or server error response),
# we can () to throw an exception by Response.raise_for_status
url = "http://httpbin.org / Status / 404 "
bad_r = requests.get (URL = URL)
Print (bad_r.status_code)
# Print (bad_r.raise_for_status ())

Response Header 11

print(r.headers) # 字典

'''
{
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Origin': '*',
'Content-Encoding': 'gzip',
'Content-Type': 'application/json',
'Date': 'Thu, 01 Aug 2019 02:59:19 GMT',
'Referrer-Policy': 'no-referrer-when-downgrade',
'Server': 'nginx',
'X-Content-Type-Options': 'nosniff',
'X-Frame-Options': 'DENY',
'X-XSS-Protection': '1; mode=block',
'Content-Length': '184',
'Connection': 'keep-alive'
}
'''

print(r.headers['Content-Type'])
print(r.headers.get('Content-Type'))

# Server can accept the same header multiple times, each time with a different value

12 cookie

# A response contains some of the cookie, you can quickly access them 
url = 'http://example.com/some/cookie/setting/url'
r = requests.get (url = url)
Print (r.cookies) # <RequestsCookieJar []>
# Print (r.cookies [ 'example_cookie_name'])

# server to send your cookies, cookies may be used parameter
URL = 'http://httpbin.org/cookies'
cookies = dict (cookies_are =' Working ')
R & lt requests.get = (URL URL =, = cookies cookies)
Print (r.text) #' { "cookies": { "cookies_are": "Working"}} '

# object returned cookies RequestsCookieJar, it and similar behavior dictionary
# cross paths for cross-domain use. You can also spread Requests in Cookie Jar

JAR = requests.cookies.RequestsCookieJar ()
jar.set ( 'tasty_cookie', 'yum', = Domain 'httpbin.org', path = '/ Cookies')
JAR.
url = 'http://httpbin.org/cookies'
r = requests.get(url=url,cookies=jar)
print(r.text) # '{"cookies": {"tasty_cookie": "yum"}}'

#

13 redirect the request history

Official Documents

 

14 out error

# Requests to stop after a number of seconds to wait for a response timeout parameter set 
# essentially all of the production code should use this parameter
# If not, your program may be lost forever response:
requests.get ( 'HTTP : //github.com ', timeout = from 0.001)
' ''
Traceback (MOST Recent Last Call):
File "<stdin>", Line. 1, in <Module1>
requests.exceptions.Timeout: HTTPConnectionPool (= Host 'GitHub. COM ', Port = 80): the Request Timed OUT (timeout = from 0.001).
' ''

'' '
timeout process only active connection, regardless of the body Download response
timeout not download the entire response time limit, but if the server does not answer within the timeout seconds, it will trigger an exception
when there is no data bytes received from the base to the socket in the second timeout
'' '

15 errors and exceptions

'' ' 
Experience network problems: when (such as DNS lookup failed, refused connections, etc.), Requests ConnectionError will throw an exception.
If the HTTP request returns an unsuccessful status code, Response.raise_for_status () will throw an exception HTTPError.
If the request times out, then throw a Timeout exception.
If the request exceeds the maximum number of redirects set will throw a TooManyRedirects exception.
All Requests explicitly throw exceptions inherit from requests.exceptions.RequestException.
'' '

 

Guess you like

Origin www.cnblogs.com/venicid/p/11284456.html