requests python 库

创建日期: 2022-09-06 17:51 | 作者: 风波 | 浏览次数: 16 | 分类: Python

官网:https://requests.readthedocs.io/en/latest/ 文档:https://requests.readthedocs.io/en/latest/user/authentication/

>>> 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, ...}
>>> 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

>>> 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')
>>> 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
from requests.auth import HTTPBasicAuth
basic = HTTPBasicAuth('user', 'pass')
requests.get('https://httpbin.org/basic-auth/user/pass', auth=basic)
16 浏览
11 爬虫
0 评论