博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python requests学习
阅读量:5265 次
发布时间:2019-06-14

本文共 3291 字,大约阅读时间需要 10 分钟。

1、无参数直接get

>>> import requests>>> r  = requests.get("http://httpbin.org/get")>>> r.status_code200>>> print(r.text){  "args": {},   "headers": {    "Accept": "*/*",     "Accept-Encoding": "gzip, deflate",     "Host": "httpbin.org",     "User-Agent": "python-requests/2.9.1"  },   "origin": "110.184.65.255",   "url": "http://httpbin.org/get"}

 

同时支持json直接获取json解码后结果

>>> r.json(){
'args': {}, 'origin': '110.184.65.255', 'url': 'http://httpbin.org/get', 'headers': {
'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.9.1'}}

 

2、有参数的get,制定params的参数的值。

>>> payload = {
'name':'felix','password':'12345678'}>>> r = requests.get('http://httpbin.org/get',params=payload)>>> print(r.url)http://httpbin.org/get?name=felix&password=12345678>>> r.status_code200>>> print(r.text){ "args": { "name": "felix", "password": "12345678" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Host": "httpbin.org", "User-Agent": "python-requests/2.9.1" }, "origin": "110.184.65.255", "url": "http://httpbin.org/get?name=felix&password=12345678"}

可以用r.url打印生成的url.

3、修改HTTP头内容

>>> headers={
'user-agent':"IE9.0"}>>> r = requests.get("http://httpbin.org/get",headers=headers)

 

4、POST方法, form格式

>>> payload{
'name': 'felix', 'password': '12345678'}>>> r = requests.post("http://httpbin.org/post",data=payload)>>> r.status_code200>>> print(r.text){ "args": {}, "data": "", "files": {}, "form": { "name": "felix", "password": "12345678" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "28", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.9.1" }, "json": null, "origin": "110.184.65.255", "url": "http://httpbin.org/post"}

json格式

>>> r = requests.post("http://httpbin.org/post",json=payload,headers=headers)>>> print(r.text){  "args": {},   "data": "{\"name\": \"felix\", \"password\": \"12345678\"}",   "files": {},   "form": {},   "headers": {    "Accept": "*/*",     "Accept-Encoding": "gzip, deflate",     "Content-Length": "41",     "Content-Type": "application/json",     "Host": "httpbin.org",     "User-Agent": "IE9.0"  },   "json": {    "name": "felix",     "password": "12345678"  },   "origin": "110.184.65.255",   "url": "http://httpbin.org/post"}

 

字符串格式:

r = requests.post('http://httpbin.org/post', data=json.dumps(payload))

 

5、超时

>>> r=requests.get("https://www.baidu.com",timeout=0.1)>>> r=requests.get("https://www.baidu.com",timeout=0.005)Traceback (most recent call last):  File "C:\Python34\lib\site-packages\requests\packages\urllib3\connection.py", line 137, in _new_conn

Errors and Exceptions

In the event of a network problem (e.g. DNS failure, refused connection, etc), Requests will raise aConnectionError exception.

In the rare event of an invalid HTTP response, Requests will raise an HTTPError exception.

If a request times out, a Timeout exception is raised.

If a request exceeds the configured number of maximum redirections, a TooManyRedirects exception is raised.

All exceptions that Requests explicitly raises inherit from requests.exceptions.RequestException.

 

更多信息详见:

 

转载于:https://www.cnblogs.com/felixr/p/5376307.html

你可能感兴趣的文章
数字视频基础(一) 分类: 生活百科 2014...
查看>>
数据库面试题目研究
查看>>
VNC 在ubuntu desktop下只显示空白桌面
查看>>
http状态码
查看>>
46. 全排列
查看>>
struts2 helloworld
查看>>
java递归算法
查看>>
资料Kernel.20181216
查看>>
ZIP压缩文件夹中上个月的文件,并将备份文件拷贝到服务器
查看>>
SQLSERVER-存储过程知识点
查看>>
Redis的安装(Centos)(转)
查看>>
mysql常用语句
查看>>
FPM 中, 找到某一个field 对应的method
查看>>
Django restful 规范
查看>>
Python虚拟环境的安装
查看>>
Linux下批量替换文件内容方法
查看>>
mssql sqlserver isnull coalesce函数用法区别说明
查看>>
hdu 1690 构图后Floyd 数据很大
查看>>
Tips on GORM, Avoid Error about "duplicate column name: id"
查看>>
黑客攻防web安全实战详解笔记
查看>>