Python requests库(一)
2019-12-07 本文已影响0人
伊洛的小屋
本文首发自伊洛的个人博客:https://yiluotalk.com,欢迎关注并查看更多内容!!!
1.Requests 库介绍
-
Requests
是一个Python
HTTP库
,在Apache 2许可证下发布。该项目的目标是使HTTP请求更简单,更人性化。
2.安装方式
- 直接使用
pip
安装
pip install requests
- 验证是否安装成功,如果可以正常导入,则说明成功
Python 3.7.5 (default, Nov 29 2019, 14:32:46)
[Clang 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>>
3.简单的例子
- 进入
python
命令行模式
➜ ~ source yiluo/bin/activate
(yiluo) ➜ ~ python
Python 3.7.5 (default, Nov 29 2019, 14:32:46)
[Clang 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
- 请求下简书的首页先
>>> import requests
>>> r =requests.get('https://www.jianshu.com/')
>>> print(r.status_code)
403
>>>
很不幸返回的状态码不是 200
- 加个
header
再尝试一下
>>> import requests
>>> headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
>>> r = requests.get('https://www.jianshu.com', headers = headers)
>>> print(r.status_code)
200
这次请求成功了,上面代码中 r
是 Response
对象,可以在这个对象中获取到我们想要的信息, 我们再输出些其他的响应内容看下
>>> import requests
>>> headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
>>> r = requests.get('https://www.jianshu.com', headers = headers)
>>> print(type(r))
<class 'requests.models.Response'>
>>> print(type(r.text))
<class 'str'>
>>> print(r.cookies)
<RequestsCookieJar[<Cookie locale=zh-CN for www.jianshu.com/>]>
如上输出了响应的类型
,响应体类型
,响应的cookies
- 其他
HTTP
请求类型PUT
,DELETE
,HEAD
以及OPTIONS
>>> r = requests.put('http://httpbin.org/put', data = {'key':'value'})
>>> r = requests.delete('http://httpbin.org/delete')
>>> r = requests.head('http://httpbin.org/get')
>>> r = requests.options('http://httpbin.org/get')
4.经常使用的请求
-
requests
构建GET
请求的方法
>>> import requests
>>> r = requests.get('http://httpbin.org/get')
>>> print(r.text)
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0"
},
"origin": "116.25.147.198, 116.25.147.198",
"url": "https://httpbin.org/get"
}
-
GET
请求传递参数
>>> import requests
>>> data = {'name':'Tom', 'age':18}
>>> r = requests.get("http://httpbin.org/get", params=data)
>>> print(r.text)
{
"args": {
"age": "18",
"name": "Tom"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0"
},
"origin": "116.25.147.198, 116.25.147.198",
"url": "https://httpbin.org/get?name=Tom&age=18"
}
-
requests
构建POST
请求的方法
>>> import requests
>>> data = {'name':'Tom', 'age':'18'}
>>> r = requests.post('http://httpbin.org/post', data = data)
>>> print(r.text)
{
"args": {},
"data": "",
"files": {},
"form": {
"age": "18",
"name": "Tom"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "15",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0"
},
"json": null,
"origin": "116.25.147.198, 116.25.147.198",
"url": "https://httpbin.org/post"
}
5.具体的一些使用方式
- 传递URL参数
Requests
允许你使用params
关键字参数,以一个字符串字典来提供这些参数
>>> import requests
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get("http://httpbin.org/get", params=payload)
>>> print(r.url)
http://httpbin.org/get?key1=value1&key2=value2
-
JSON
响应内容
Requests
内置的JSON
解码器,处理JSON
数据:
>>> import requests
>>> r = requests.get('https://api.github.com/events')
>>> r.json()
- 定制请求头
为请求添加HTTP
头部,只要传递一个dict
给headers
参数就可以
>>> import requests
>>> url = 'https://api.github.com/some/endpoint'
>>> headers = {'user-agent': 'my-app/0.0.1'}
>>> r = requests.get(url, headers=headers)
......待续
享受每一天,Just Enjoy !