requests库的用法
使用requests模拟浏览器发出请求
安装
pip isntall requests
官方文档:https://requests.readthedocs.io/en/master/
中文文档:https://requests.readthedocs.io/zh_CN/latest/
requests库常用方法:
requests.requests()
requests.get(‘https://github.com/timeline.json’) #GET请求
requests.post(“http://httpbin.org/post”) #POST请求
requests.put(“http://httpbin.org/put”) #PUT请求(提交修改全部的数据)
requests.delete(“http://httpbin.org/delete”) #DELETE请求
requests.head(“http://httpbin.org/get”) #HEAD请求
requests.patch(“http://httpbin.org/get”) #PATCH请求(提交修改部分数据)
#### 带参数GET请求
GET 请求,附加额外的信息
在URL后面拼接,用一个?来分割一下,参数传过来然后用&的符号来进行分割,比如现在想添加两个参数,其中name是germery,age是22.构造这个请求链接
```python
r = requests.get('http://httpbin.org/get?name=germery&age=22')
import requests
data = {
'name':'germary',
'age':22
}
r = requests.get('http://httpbin.org/get',params=data)
print(r.text)
headers: 字典,HTTP定制头
data: 是第二个控制参数,向服务器提交数据
`import` `requests``import` `json`` ` `data ``=` `{``'some'``: ``'data'``}``headers ``=` `{``'content-type'``: ``'application/json'``,`` ``'User-Agent'``: ``'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'``}`` ` `r ``=` `requests.post(``'https://api.github.com/some/endpoint'``, data``=``data, headers``=``headers)``print``(r.text)`