Reptile Learning (II) use requests module

A, requests Overview

requests module is a network request, returns the response data . The underlying implementation is urllib, and easy to use, in python2, python3 in general, can automatically help us unpack (gzip compression, etc.) web content.

 

Second, the basic requests of use

1, the basic use:

  • Module installation requests: pip install requests
  • Import module:import reqeusts
  • Sending a request acquisition response: response = requests.get (url)
  • Retrieve data from the response

2. Method:

(1) requests.get (url, params = None, ** kwargs), sends a get request, returns a Response object

  • url: url requests
  • params: get behind the requested optional parameters dictionary?
    One way: stitching myself a URL with parameters, such as "https://www.sogou.com/web?query={}"
    Second way: When sending a request to use params specified formatrequests.get("url", params={})
  • ** kwargs: Optional Parameters
    headers : request header parameter dictionary format requests.get (url, headers = {} )

(2) requests.post (url, data = None, json = None, ** kwargs), send a request to post

3. Object:

(1) Response object is an object in response to the transmission request

Common attributes:

  • Response.text: str type response data
  • Response.content: binary type of response data
  • Response.status_code: Response Status Code
  • Response.headers: response header
  • Response.request.headers: request header

 

Guess you like

Origin www.cnblogs.com/chjxbt/p/11359679.html