码迷,mamicode.com
首页 > 其他好文 > 详细

Requests库详解

时间:2018-06-09 15:59:08      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:简单   3.1   python-r   model   python实现   head   ons   还需要   小程序   

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"}

 

 

Requests库详解

标签:简单   3.1   python-r   model   python实现   head   ons   还需要   小程序   

原文地址:https://www.cnblogs.com/ronghe/p/9159686.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!