关闭

Python下的Request库

646人阅读 评论(0) 收藏 举报
分类:

需要用到网页获取,想到了之前用过的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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:145230次
    • 积分:2432
    • 等级:
    • 排名:第15231名
    • 原创:111篇
    • 转载:19篇
    • 译文:0篇
    • 评论:20条
    文章分类
    最新评论