GuoXin Li's Blog

Requests' details of Python

字数统计: 338阅读时长: 2 min
2018/08/10 Share

The methods of Requests

requests.* details
requests.request() Construct a request to support the following basel methods
requests.get(url,params=None,**kwargs) url: links; params: url中的额外参数,字典或者字节流格式,可选; **kwargs: 12个控制访问的参数,可选;
requests.head() get the head info of HTML, to HTTP’s HEAD
requests.post() submit post to HTML, to HTTP’s POST
requests.put() submit PUT to HTML, to HTTP’s PUT
requests.patch() submit the modification request, to HTTP’s PATCH
requests.delete() submit the delete to HTML, to HTML’s DELETE

The objects of Requests

  • Response:

  • Request:

The properties of Response Details
r.status_code the status of HTTP’s response, 200 is right, 404 wrong;
r.text the alphabetic sting of HTTP’s response content, is the content of url;
r.encoding the coding scheme from the guest of HTTP header
r.apparent_encoding the coding scheme from the analisis of content of url
r.content the binary scheme of HTTP’s response

The general code framework of reptile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import requests

def getHTMLText(url):
try:
r = requests.get(url,timeout = 30)
r.raise_for_status()#if time not 200, return false;
r.encoding = r.apparent_encoding
return r.text
except:
return "failed"

if __name__ == "__main__":
url = "https://www.google.com"
print(getHTMLText(url))

The excepting handing of Requests

excepting details
request.ConnectionError internet connection error, e.g.: DNS find error, reject connection
requests.HTTPError HTTP false error
requests.URLRequired deficiency error of URL
requests.TooManyRedirects error: over the redirect time
requests.ConnectTimeout timeout when connect the service
requests.Timeout timeout when get url

A method of error to Requests

r.raise_for_status() : if response not 200; return requests.HTTPError

e.g.: last step

Http Protocol:

HTTP,Hypertext Transfer Protocol, 超文本传输协议

The format of URL:

URL: http://host[:port][path]

  • host: the legal internet host name or ip address

  • port: the port number, default: 80

  • path: the source path of response

CATALOG
  1. 1. The methods of Requests
  2. 2. The objects of Requests
  3. 3. The excepting handing of Requests
  4. 4. Http Protocol:
  5. 5. The format of URL: