>>> import requests
>>> r=requests.head("http://www.baidu.com")
>>> r.headers
{'Connection': 'close', 'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Content-Encoding': 'gzip', 'Content-Type': 'text/html', 'Date': 'Mon, 31 Jul 2017 13:23:10 GMT', 'Keep-Alive': 'timeout=38', 'Last-Modified': 'Mon, 13 Jun 2016 02:50:23 GMT', 'Pragma': 'no-cache', 'Server': 'bfe/1.0.8.18'}
r=requests.post(url,data=None,Json=None,**kwgras)
通常,你想要发送一些编码为表单形式的数据——非常像一个 HTML 表单。要实现这个,只需简单地传递一个字典给 data 参数。你的数据字典在发出请求时会自动编码为表单形式:
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("http://httpbin.org/post", data=payload)
>>> print(r.text)
{
...
"form": {
"key2": "value2",
"key1": "value1"
},
...
}
Requests 使得上传多部分编码文件变得很简单:
>>> url = 'http://httpbin.org/post'
>>> files = {'file': open('report.xls', 'rb')}
>>> r = requests.post(url, files=files)
>>> r.text
{
...
"files": {
"file": "<censored...binary...data>"
},
...
}
向HTML网页提交PUT请求的方法,对应于HTTP的PUT
r=requests.put(url,data=None,**kwgras)
import requests
r=requests.put("http://www.baidu.com")
r.text
>>>'<html>\r\n<head>\r\n<meta http-equiv="content-type" content="text/html;charset=utf-8">\r\n<style data-for='
向HTML网页提交局部修改请求,对应于HTTP的PATCH
r=requests.patch(url,data=None,**kwgras)
import requests
r=requests.patch("http://www.zhihu.com")
r.text
>>>'<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n'
向HTML页面提交删除请求,对应于HTTP的DELETE
r=requests.put(url,**kwgras)
import requests
r=requests.delete("http://www.zhihu.com")
r.text
Source From:Requests:HTTP for humans