Requests is an HTTP library written in Python , based on Urllib, using the Apache2 Licensed Open source protocol. It is more convenient than urllib, it can save us a lot of work, fully meet the requirements of HTTP testing. Requests's philosophy was developed in the center of the PEP 20 idiom, so it is more pythoner than Urllib. The more important thing is that it supports Python3 Oh!
- Beautiful is better than ugly. (Beauty is better than ugliness)
- Explicit is better than implicit. (clearly superior to ambiguity)
- Simple is better than complex. (simpler than complex)
- Complex is better than complicated. (more complex than cumbersome)
- Readability counts. (Important is readability)
First, install the requests
1. Installing PIP install requests via PIP
2. Using the IDE installation such as Pycharm
Second, send the request get and pass the post parameters
Let's start with a simple example! Let you know the power of it:
1# r = requests.Get(url='http://www.itwhy.org') #最基本的get请求2 # print R.status_code #返回状态3# r= requests.Get(url='http://www.baidu.com/',params={'WD':'python'}) #带参数的get请求4 # Print R.url5# Print R.text #d打印解码后的返回数据
HTTP not only get so simple, other way is also a sentence OH
1 Get= requests.Get('http://httpbin.org') #get请求2Post = Requests.post ('Https://httpbin.org/post') #post请求3put = Requests.put ('Http://httpbin.org/put') #put请求4Delete = Requests.delete ('Http://httpbin.org/delete') #delete Request5Head = Requests.head ('Http://httpbin.org/get') #head请求6Options = Requests.options ('Http://httpbin.org/get') #option Request
Already on the HTTP method, for the web system generally only support get and post some also support head method.
1 #带参数的请求实例2 3Requests.Get('http://www.dict.baidu.com/s',params={'WD':'python'})4Requests.post ('http://www.itwhy.org/wp-comments-post.php', data = {'Comment':'Test Post'})
Several methods of post transmitting data
If we are going to send a complex form, we need the post data. As with get transfer data, you want to add an additional data parameter to the method. This is the equivalent of filling in the form with the data and then clicking on the form's submission.
1. Submit form data
1data = {2 'name':'Yitian',3 ' Age': A,4 'Friends':['Zhang3','Li4'] #创建一个data表单数据5 }6Response = Requests.post ('{base_url}post', Data=data) #传入一个字典data
2. Submit String Data
Sometimes the post data is not used as a form, but instead is attached directly to the request body. Then we cannot add a dictionary to the data parameter when we send the parameter, but we should pass the string.
1 response = requests.post ('{base_url}post', Data=json.dumps (data)) # The example above is to convert the data dictionary to a JSON structure
Json.loads (): Transforming the JSON structure into a python data structure
Json.dumps (): Transform Python data structure into JSON structure
Json.load (): Read JSON file into Python data structure
Json.dump (): Writes a JSON file, reads the JSON file, and then turns into a python data structure
3. Direct commit JSON string as a whole transfer
Some programs, such as GitHub's API, need to send JSON strings directly to the request body, such as the above example of converting a dictionary to JSON. In this case, we can pass the dictionary reference directly to the JSON parameter of the method, so that we do not need to manually convert, requests will automatically convert.
1 response = requests.post ('{base_url}post' )
4. Uploading Files
On the Web page, uploading avatars and other operations requires uploading multipart/form-data types of forms. Using requests is also very simple. Note that it is best to use binary mode when opening a file, and opening a file using text mode may cause requests not to correctly calculate the size of the file.
File = open (R'c:\Windows\System32\drivers\etc\hosts', mode='RB' ) = { 'file'= Requests.post (f ' {base_url}post ', files=data) print (Response.text)
Requests supports streaming uploads, allowing large data streams or files to be sent without first reading them into memory. Need to use streaming uploads
1 with open ('massive-body') as F:2 requests.post (' http://some.url/streamed', data=f)
Cookie manipulation
If you want to get a cookie for the response, call the cookie attribute and it will return an RequestsCookieJar object that implements the Http.cookiejar of the standard library. So we can follow Cookiejar's
method to use RequestsCookieJar . When visiting Baidu, for example, it assigns a cookie, so we can use the following code to get a cookie.
1 cookies = Requests.cookies.RequestsCookieJar ()2 cookies.set ('name ','yitian')3 response = Requests.get ('{ Base_urlcookies', cookies=cookies)4print response.text
10.python-third-party library requests details (II.)