Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the third-party library of requests in testing

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use the third-party library of requests in the test. I hope you will get something after reading this article. Let's discuss it together.

1.requests Library documentation links

Official document: http://www.python-requests.org/en/master/

Third-party Chinese document: http://docs.python-requests.org/zh_CN/latest/user/quickstart.html

two。 Send a get request using requests

To send a get request using requests, you first need to install the requests module, and then import and use

Pip install requestsimport requests

Then we can use, for example: r = requests.get ('https://www.baidu.com'), method to get a web page, r represents a Response object, from which we get all the information we want

Import requestsfrom requests.cookies import RequestsCookieJarr = requests.get ('https://www.baidu.com')3. How to pass url parameters

If we want to build URl manually, we can use the params keyword to pass parameters, such as:

R = requests.get ('https://www.baidu.com'), params= {' id': 'abc'}) print (r.url) # output: https://www.baidu.com/?id=abc

So we can know that if the URL is built manually, the data will be placed in the URL as a key / value pair, followed by a question mark

Response response information commonly used in 4.resquest

R.text: returns the response object, Unicode data, mainly text

R = requests.get ('https://www.baidu.com')print(r.text)

R.content: return response object, bytes, binary data, mainly take pictures and files, etc., displayed as characters in Chinese

R.url: gets the url of the current request

R.json (): JSON decoder built into Requests

R.encoding: encoding method

R.status_code: status response code

R.headers: response header

R.cookies: returns cookie

5. Send a post request using requests

There are generally three types of coding methods for POST requests, that is, Content-Type, which are:

Application/x-www-form-urlencoded: submit data as a form form. When you use it, you only need to construct a dictionary of the requested parameters and pass them to the data parameter of requests.post ().

Import requestsfrom requests.cookies import RequestsCookieJarpayload = {'key1':' value1', 'key2':' value2'} r = requests.post ("http://httpbin.org/post", data=payload) print (r.text) # the headers in the output print is" Content-Type ":" application/x-www-form-urlencoded ", so we can know that application/x-www-form-urlencoded is the default Content-Type for post requests.

Application/json: submit data in json format, which is mainly used to dynamically load data in sending ajax requests

Multipart/form-data: generally used for uploading files. When using, you need to transfer the files to the files parameter of requests.post ().

6. Timeout

To prevent the server from responding slowly and causing the client to handle exceptions, most requests requests set the timeout parameter, such as requests.get ('http://baidu.com', timeout=10). If you need to keep request waiting forever, pass in a None as the value of timeout

Import requestsfrom requests.cookies import RequestsCookieJarr = requests.get ('https://www.baidu.com', timeout=0.001) print (r) # will report an error when executing code, because the server response has not completed 7.Cookie at 0.001 seconds

The returned object of Cookie is RequestsCookieJar, which behaves like a dictionary, but the interface is more complete and suitable for cross-domain name and cross-path use. You can send CookieJar to Requests to request login page, for example: r = requests.get ('[https://www.baidu.com/',](https://www.baidu.com/',) cookies=jar)

Let's try to use cookie to get the information after logging in to Douban's official website.

Get cookie

From requests.sessions import Session# returns the context manager object requests = Session () headers = {'User-Agent':' Mozilla/5.0 (Windows NT 6.1; Win64) for a session X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'} date = {'ck':'',' name':'xxxxxxxx', 'password':' xxxxxxxx', 'remember':'false',' ticket':''} result = requests.post ('https://accounts.douban.com/j/mobile/login/basic', data=date Headers=headers) print (result.text) # get cookiecookies = result.cookiesfor i in cookies: print (I) for item in cookies.iteritems (): print (item) print (cookies.get_dict ()) print (cookies.list_paths ()) print (cookies.list_domains ()) print (cookies.items ())

Get the login page using cookie request

From requests.cookies import RequestsCookieJarimport requestsjar = RequestsCookieJar () jar.set ('bid',' OvpnenMUNWo', domain='.douban.com', path='/') jar.set ('dbcl2', r' "153039786:aYRnqJukECk", domain='.douban.com', path='/') r = requests.get ('https://www.douban.com/', cookies=jar) print (r.text) after reading this article, I believe you have some understanding of "how to use requests third-party libraries in testing" If you want to know more related knowledge, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report