Python下的Request库

本文介绍了Requests库的基本使用方法,包括常见的HTTP请求方式如GET、POST、PUT和DELETE等,并展示了如何利用该库处理JSON数据和上传文件。此外还列举了Requests库的一些特性,如连接池管理、会话保持、SSL验证等功能。
部署运行你感兴趣的模型镜像

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

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值