Requests库详解

urllib库作为基本库,requests库也是在urllib库基础上发展的

但是urllib在使用上不如requests便利,比如上篇文章在写urllib库的时候,比如代理设置,处理cookie时,没有写,因为感觉比较繁琐,另外在发送post请求的时候,也是比较繁琐。

一言而代之,requests库是python实现的简单易用的HTTP库

在以后做爬虫的时候,建议用requests,不用urllib

用法讲解:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import requests

response = requests.get('http://www.baidu.com')
print(type(response))
print(response.status_code)
print(type(response.text))
print(response.text)
print(response.cookies)
输出结果为:

<class 'requests.models.Response'>
200
<class 'str'>
<!DOCTYPE html>
<!--STATUS OK--><html>省略了 </html>

<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>

由上面的小程序中可以看出,response.text直接就拿到str类型的数据,而在urllib中的urllib.request.read()得到的是bytes类型的数据,还需要decode,比较繁琐,同样的response.cookies直接就将cookies拿到了,而不像在urllib中那样繁琐

各种请求方式

import requests

requests.post('http://www.baidu.com')
requests.put('http://www.baidu.com')
requests.delete('http://www.baidu.com')
requests.head('http://www.baidu.com')
requests.options('http://www.baidu.com')

 基本get请求:

利用http://httpbin.org/get进行get请求测试:

import requests

response = requests.get('http://httpbin.org/get')
print(response.text)
输出结果:

{"args":{},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.4"},"origin":"113.71.243.133","url":"http://httpbin.org/get"}

扫描二维码关注公众号,回复: 1539494 查看本文章

带参数的get请求:

import requests

data = {'name':'geme','age':'22'}
response = requests.get('http://httpbin.org/get',params=data)
print(response.text)
输出结果为:

{"args":{"age":"22","name":"geme"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.4"},"origin":"113.71.243.133","url":"http://httpbin.org/get?age=22&name=geme"}

解析json  

response.text返回的其实是json形式的字符串,可以通过response.json()直接进行解析,解析结果与json模块loads方法得到的结果是完全一样的

 

猜你喜欢

转载自www.cnblogs.com/ronghe/p/9159686.html