Requests Library Introduction

Requests libraries installed

1, using the administrator to open cmd, enter install requests PIP
2, you can use pip list to see whether there have been requests library libraries have been installed, there is also a direct reference to the IDE, see if you can correctly quote

Requests Library 7 primary method

method Explanation
requests.request() A configuration request, the following method of supporting foundation method
requests.get() Get HTML pages of the main methods, corresponding to the HTTP GET
requests.head() HTML page header information obtaining method corresponding to the HTTP HEAD
requests.post() A method to post requests submitted HTML pages, corresponding to the HTTP POST
requests.put() PUT method to submit a request to the HTML page corresponding to the HTTP PUT
requests.patch() Local modification request to submit an HTML page corresponding to the HTTP PATCH
requests.delete() Submit a request to delete HTML pages, corresponding to the HTTP DELETE

Requests object attributes 5

Attributes Explanation
r.ststus_code HTTP return status of the request, 200 indicates a successful connection, connection failure 404
r.text String HTTP response content, both, url corresponding page content
r.encoding HTTP head guessed from the encoding response content
r.apparent_encoding Analysis of the content of the response from the content encoding (encoding alternatively)
r.content HTTP response binary form content

Requests to understand abnormal library

abnormal Explanation
requests.ConnectionError Network connection error exceptions, such as DNS query failed, refused connections
requests.HTTPError HTTP error exception
requests.URLRequired URL missing abnormal
requests.TooManyRedirects Exceeds the maximum number of redirects, redirect produce abnormal
requests.ConnectTimeout Connect to a remote server timeout exception
requests.Timeout URL request times out, resulting in a timeout exception

Crawled pages generic code frame

import requests

def getHTMLText (url):
    try:
        r = requests.get(url,timeout = 30)
        r.raise_for_status()  #如果状态不是200,引发HTTPError异常
        r.encoding = r.apparent_encoding #将apparent_encoding替代encoding,使返回解码的格式是正常的
        return r.text
    except:
        return "产生异常"

if __name__ == "__main__":
    url = "http://www.baidu.com"
    print(getHTMLText(url))

Methods Introduction

  • . 1, GET ()
    R & lt requests.get = (url, the params = None, ** kwargs)
    url: Quasi acquired page url connection
    params: url additional parameters, or byte stream format dictionary, optional
    ** kwargs: 12 access control parameters

Example:

import requests

r = requests.get("http://www.baidu.com")
print(r.status_code)

返回结果:
200
  • 2, POST ()
    requests.post (url, Data = None, json = None, ** kwargs)
    url: url intend to update the page link
    data: dictionary, a sequence of bytes or a file, request contents
    json: data format json , request content
    ** kwargs: 11 access control parameters
    Example 1:
import requests

payload = {"key1":"value1","key2":"value2"}
r = requests.post("http://httpbin.org/post",data=payload)
print(r.text)

结果:
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "key1": "value1", 
    "key2": "value2"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "23", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.22.0"
  }, 
  "json": null, 
  "origin": "183.40.12.54, 183.40.12.54", 
  "url": "https://httpbin.org/post"

There you can see the result, the URL POST a dictionary, automatic coding for the form (form)

Example 2:

import requests


r = requests.post("http://httpbin.org/post",data="ABC")
print(r.text)

结果:
{
  "args": {}, 
  "data": "ABC", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "3", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.22.0"
  }, 
  "json": null, 
  "origin": "183.40.12.54, 183.40.12.54", 
  "url": "https://httpbin.org/post"

There you can see the result, the URL POST a dictionary, data is automatically encoded as

  • 3, head ()
    requests.head (url, ** kwargs)
    url: the proposed acquisition of the page url link
    ** kwargs: 13 access control parameters
import requests


r = requests.head("http://httpbin.org/get")
print(r.headers)

结果:
{'Access-Control-Allow-Credentials': 'true', 'Access-Control-Allow-Origin': '*', 'Content-Encoding': 'gzip', 'Content-Type': 'application/json', 'Date': 'Fri, 03 Jan 2020 08:35:11 GMT', 'Referrer-Policy': 'no-referrer-when-downgrade', 'Server': 'nginx', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'DENY', 'X-XSS-Protection': '1; mode=block', 'Connection': 'keep-alive'}

  • . 4, PUT ()
    requests.put (url, Data = None, ** kwargs)
    url: url link page intended to obtain
    data: dictionary, a sequence of bytes or a file, request contents
    ** kwargs: 12 access control parameters
import requests

payload = {"key1":"value1","key2":"value2"}
r = requests.put("http://httpbin.org/put",data=payload)
print(r.text)

结果:
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "key1": "value1", 
    "key2": "value2"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "23", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.22.0"
  }, 
  "json": null, 
  "origin": "183.40.12.54, 183.40.12.54", 
  "url": "https://httpbin.org/put"
}
  • . 5, Patch
    request.patch (url, Data = None, ** kwargs)
    url: url link page intended to obtain
    data: dictionary, a sequence of bytes or a file, request contents
    ** kwargs: 12 access control parameters

  • 6, the Delete
    request.delete (url, ** kwargs)
    url: the proposed acquisition of the page url link
    ** kwargs: 13 access control parameters

Requests Library main analytical method

requests.request (Method, url, ** kwargs)
Method: mode request, the corresponding get / put seven kinds
url: url of the page acquired connection
** kwargs: access control parameters, a total of 13

Of parameters:
the params: byte dictionary or a sequence, as added to the url parameter
data: dictionary file object or byte sequence, as the content of the request
json: json data format, as the content of the request
headers: dictionary, HTTP header customization
cookies: a dictionary or cookiesjar, request in the cookie
auth: tuples, support http authentication
files: a dictionary, transmission authentication
timeout: Specifies the timeout, in seconds
proxies: a dictionary, set the access proxy server, you can increase login authentication
allow_redirects: True / False, the default is True, redirect switch
stream: True / False, the default is True, access to content downloads switch immediately
verify: True / False, the default is True, SSL certificate authentication switch
cert: local SSL certificate path

r = requests.request(“GET”,url,**kwargs)
r = requests.request(“HEAD”,url,**kwargs)
r = requests.request(“POST”,url,**kwargs)
r = requests.request(“PUT”,url,**kwargs)
r = requests.request(“PATCH”,url,**kwargs)
r = requests.request(“DELETE”,url,**kwargs)
r = requests.request(“OPTIONS”,url,**kwargs)

HTTP protocol

HTTP (Hypertext Transfer Protocol): hypertext transfer protocol
http is based on a "request corresponding to" model, stateless application layer protocol

URL format: HTTP: // Host [: Port] [path]
Host: legal Internet host domain name or IP address
port: port number, the default port is 80
path: the path of the requested resource

Appreciated that the HTTP URL: URL is the HTTP protocol to access the resource through Internet path, a resource URL corresponding to a data

HTTP protocol operations for resources

method Explanation
GET Request URL location of resources
HEAD Request resource URL location report response message, to obtain both the header information of the resource
POST After the request for additional resources to the URL location of the new data
PUT URL request to the storage location of a resource, covering the resources of the original URL location
PATCH Partial update URL request resource position, where both the part of the resource changes
DELETE A request to delete the URL location of storage resources

PATCH and PUT understand the difference between the
assumed position of a set of data URL UserInfo, including UserID, UserName field 20 and other
requirements: the user to modify the UserName, other unchanged
use PATCH, UserName submitted only partial update request to the URL
using the PUT, must be All 20 fields to be submitted to the URL, uncommitted field is removed

Published 44 original articles · won praise 1 · views 1461

Guess you like

Origin blog.csdn.net/cc_park/article/details/103819445