Python下的Request库

需要用到网页获取,想到了之前用过的urllib2。由于是几年前用的,也想找找有没有替代品,刚好试试Request吧。
主页在这里:http://docs.python-requests.org/en/latest/

这里的答案对Request很推崇,列举了很多优点:

First, it supports a fully restful API, and is as easy as:

import requests
...

req = requests.get('http://www.mywebsite.com/user')
req = requests.post('http://www.mywebsite.com/user')
req = requests.put('http://www.mywebsite.com/user/put')
req = requests.delete('http://www.mywebsite.com/user/delete')

从代码来看确实很简洁,同时使用字典作为提交的参数

Regardless of whether GET/POST you never have to encode parameters again, it simply takes a dictionary as an argument and is good to go.

userdata = {"firstname": "John", "lastname": "Doe", "password": "jdoe123"}
req = requests.post('http://www.mywebsite.com/user', params=userdata)

内建了json解码器,读取json格式更简便

Plus it even has a built in json decoder (again, i know json.loads() isn’t a lot more to write, but this sure is convenient):

resp.json()

如果处理的数据是文本数据,可以直接使用req.text得到。

官网上也列举了很多有用的特征
- International Domains and URLs
- Keep-Alive & Connection Pooling
- Sessions with Cookie Persistence
- Browser-style SSL Verification
- Basic/Digest Authentication
- Elegant Key/Value Cookies
- Automatic Decompression
- Unicode Response Bodies
- Multipart File Uploads
- Connection Timeouts
- .netrc support
- List item
- Python 2.6—3.4
- Thread-safe.

ok,看着还不错,试试吧。

安装
使用pip安装:

>pip install requests

此时安装最新版本是2.7.0.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值