brief introduction
- Requests is an elegant and easy-to-use HTTP library, built specifically for humans
- This article focuses on requests 2.x
- The authors of requests are Kenneth Reitz , to focus on requests3 Development, Kenneth Reitz has requests2 Hand over to PSF (Python Software Foundation) Administration
- PyPI address: https://pypi.org/project/requests/
Article environment
Windows 10 Python 3.6.x Requests 2.x
install
pip install requests -i https://mirrors.aliyun.com/pypi/simple/
Tips and examples
cookie
- Add cookie
requests.utils.add_dict_to_cookiejar(cj, cookie_dict)
- If sn.headers['Cookie '] has a value, then sn.cookies no longer work
Close https certificate warning
import requests from urllib3.exceptions import InsecureRequestWarning # Close https certificate warning requests.urllib3.disable_warnings(InsecureRequestWarning)
Advanced Usage
Default timeout
- socket.getdefaulttimeout()
- When using the requests library, what is the maximum timeout value that can be set?
get url 25%
The percentage sign (%) in the url when Requests are get is forced to be encoded as 25%, which can be bypassed as follows:
import requests sn = requests.Session() url = 'http://xxx.net/xxx.aspx?Param=ASP.brief_result_aspx%23/%u5E74 req = requests.Request('GET', url) prepped = sn.prepare_request(req) prepped.url = prepped.url.replace('%25', '%') r = sn.send(prepped)
Or reassemble after splitting:
import requests from urllib import parse url = 'http://xxx.net/xxx.aspx?' + parse.urlencode(dict(parse.parse_qsl(parse.urlparse(url).query))) r = sn.get(url)
POST request
- POST a Multipart-Encoded File
- About Request Payload. ( POST a Multipart-Encoded File)
[code]
import requests import json r = requests.post('http://www.baidu.com', files={'key':'val'}) print('***test 1: %s' % r.request.body) print('------------------------') print(r.request.body.decode('utf8')) print('\n') line = json.dumps({'k1':'v1', 'k2':'v2'}) r = requests.post('http://www.baidu.com', files={'json':(None, line)}) print('***test 2: %s' % r.request.body) print('------------------------') print(r.request.body.decode('utf8'))
[output]
***test 1: b'--5b57f36c03ca462f93dfbd8dfc97e2d1\r\nContent-Disposition: form-data; name="key"; filename="key"\r\n\r\nval\r\n--5b57f36c03ca462f93dfbd8dfc97e2d1--\r\n' ------------------------ --5b57f36c03ca462f93dfbd8dfc97e2d1 Content-Disposition: form-data; name="key"; filename="key" val --5b57f36c03ca462f93dfbd8dfc97e2d1-- ***test 2: b'--cf66a355e1f6441fa5a3079e1fafc43a\r\nContent-Disposition: form-data; name="json"\r\n\r\n{"k1": "v1", "k2": "v2"}\r\n--cf66a355e1f6441fa5a3079e1fafc43a--\r\n' ------------------------ --cf66a355e1f6441fa5a3079e1fafc43a Content-Disposition: form-data; name="json" {"k1": "v1", "k2": "v2"} --cf66a355e1f6441fa5a3079e1fafc43a--
This article is from walker snapshot