官网:https://requests.readthedocs.io/en/latest/ 文档:https://requests.readthedocs.io/en/latest/user/authentication/
- GET
>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
'{"type":"User"...'
>>> r.json()
{'private_gists': 419, 'total_private_repos': 77, ...}
- POST
>>> r = requests.post('https://httpbin.org/post', data={'key': 'value'})
其它的一个参考:https://stackoverflow.com/questions/52351392/send-key-value-form-data-using-post-request-in-python
r = request.post('https://www.dmv.ca.gov/wasapp/foa/findDriveTest.do',data=values)
The reason that your previous code didn't make a POST request is that you were trying to pass your values as URL parameters which is used when making a GET request
- PUT, DELETE, HEAD and OPTIONS
>>> r = requests.put('https://httpbin.org/put', data={'key': 'value'})
>>> r = requests.delete('https://httpbin.org/delete')
>>> r = requests.head('https://httpbin.org/get')
>>> r = requests.options('https://httpbin.org/get')
- URL 参数
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get('https://httpbin.org/get', params=payload)
>>> print(r.url)
https://httpbin.org/get?key2=value2&key1=value1
- BASIC AUTH
from requests.auth import HTTPBasicAuth
basic = HTTPBasicAuth('user', 'pass')
requests.get('https://httpbin.org/basic-auth/user/pass', auth=basic)