博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python的requests模块参数详解
阅读量:6233 次
发布时间:2019-06-21

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

import requestsprint(dir(requests))# 1、方法# ['ConnectTimeout', 'ConnectionError', 'DependencyWarning', 'FileModeWarning', 'HTTPError', 'NullHandler', 'PreparedRequest', 'ReadTimeout', 'Request', 'RequestException', 'RequestsDependencyWarning', 'Response', 'Session', 'Timeout', 'TooManyRedirects', 'URLRequired', '__author__', '__author_email__', '__build__', '__builtins__', '__cached__', '__cake__', '__copyright__', '__description__', '__doc__', '__file__', '__license__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__title__', '__url__', '__version__', '_check_cryptography', '_internal_utils', 'adapters', 'api', 'auth', 'certs', 'chardet', 'check_compatibility', 'codes', 'compat', 'cookies', 'delete', 'exceptions', 'get', 'head', 'hooks', 'logging', 'models', 'options', 'packages', 'patch', 'post', 'put', 'request', 'session', 'sessions', 'status_codes', 'structures', 'urllib3', 'utils', 'warnings']# 2、参数requests.get(    url="http://www.baidu.com",    headers="",    cookies="",    params={"k1":"v1","k2":"v2"},    # url中传递的参数,效果如下    # http://www.baidu.com?k1=v1&k2=v2)requests.post(    url="",    headers="",    cookies="",    data={    },    params={"k1": "v1", "k2": "v2"},    # url中传递的参数,效果如下    # http://www.baidu.com?k1=v1&k2=v2)# 我们可以通过data传递请求体,也可以通过json传递请求体data = {        "username":"admin",        "pwd":"admin"       },# 则请求体中的数据为username=admin&pwd=admin# 参数jsonjson = {        "username":"admin",        "pwd":"admin"       },# 则请求体中的数据为{"username":"admin","pwd":"admin"}# 参数代理    # 定义一个字典proxies = {    "http":"61.24.25.21",    "https":"http://65.21.24.1"}# http请求走http对应的地址,https请求走https对应的地址,在访问的请求中加一个proxies的参数l1 = requests.get(url="https://passport.lagou.com/login/login.html",                  headers={                      "user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"                  },                  proxies = proxies                  )# 给代理加认证from requests.auth import HTTPProxyAuthproxies = {    "http":"61.24.25.21",    "https":"http://65.21.24.1"}auth = HTTPProxyAuth("username","passwd")# http请求走http对应的地址,https请求走https对应的地址,在访问的请求中加一个proxies的参数,在加一个参数auth,这个是登陆代理的用户名和密码l2 = requests.get(url="https://passport.lagou.com/login/login.html",                  headers={                      "user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"                  },                  proxies = proxies,                  auth = auth                  )# 参数文件上传,post方法发送请求,传递一个file的参数file= {    "f1":open("a.txt","rb")}l3 = requests.post(url="https://passport.lagou.com/login/login.html",                  headers={                      "user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"                  },                  proxies = proxies,                  auth = auth,                  file = file                  )# 可以设置上传文件的名称,前面的例子上传的文件的名称就是文件本身的名称file= {    "f1":("new_file_name",open("a.txt","rb"))}l4 = requests.post(url="https://passport.lagou.com/login/login.html",                  headers={                      "user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"                  },                  proxies = proxies,                  auth = auth,                  file = file                  )# 参数认证from requests.auth import HTTPBasicAuthfrom requests.auth import HTTPDigestAuthl5 = requests.get(url="https://passport.lagou.com/login/login.html",                  headers={                      "user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"                  },                  proxies = proxies,                  auth = HTTPBasicAuth("admin","admin")                  )# 超时参数l6 = requests.get(url="https://passport.lagou.com/login/login.html",                  headers={                      "user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"                  },                  timeout = 2                  )# 超时时间为2s,2s连不上返回错误# 允许重定向l7 = requests.get(url="https://passport.lagou.com/login/login.html",                  headers={                      "user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"                  },                  allow_redirects = False                  )# stream大文件下载的参数,把文件一点一点的下载,如果这个值为false,则全部写到内存中了from contextlib import closingwith closing(requests.get("http://ddddddd",stream=True)) as f:    for i in f.iter_content():        print(i)# cert,证书参数,告诉request去这个地方去下载certl8 = requests.get(url="https://passport.lagou.com/login/login.html",cert="xxx/xxx/xxx/xxx/pem")l9 = requests.get(url="https://passport.lagou.com/login/login.html",cert=("xxx/xxx/xxx/xxx/pem","yyy/yyy/yyy.key"))# session,为我们自动带上cookies和请求头import requestssession = requests.session()i1 = session.get(url="")i2 = session.post(    url="",    data={})i3 = session.post()

  

 

 

 

----------------------------------------------------------

通过request发送post请求,什么时候使用data参数,什么时候使用json参数呢,可以通过抓包来分析

 

在chrom浏览器中,数据格式为Form Data,如果通过requests发送数据,则用data来发送数据

在chrom浏览器中,数据格式为Request Payload,如果通过requests发送,则用json来发送数据

 

如果传递的json格式,但是数据有中文呢就额可以使用下面的方式来发送数据

data = bytes(json.dumps(                data_dict,                ensure_ascii=False            ),encoding="utf-8")

 

转载于:https://www.cnblogs.com/bainianminguo/p/10674396.html

你可能感兴趣的文章
Mysql Date 函数
查看>>
博客分类导航
查看>>
实战HMM-Viterbi角色标注地名识别
查看>>
Delphi 与 DirectX 之 DelphiX(58): TDIB.DoTile();
查看>>
如何打开记事本并显示指定内容 - 回复 "苦苦苦" 的问题
查看>>
android4.x新特征总结
查看>>
TClientDataSet[22]: 数组字段与 ObjectView
查看>>
Oracle Data Guard(1)
查看>>
我的友情链接
查看>>
实现iOS图片等资源文件的热更新化(三):动态的资源文件夹
查看>>
OK6410-使用DirecetFB支持Qt4.7.0
查看>>
python获取linux系统信息、性能阀值、短信网关发送的例子
查看>>
微信公众号实现回复图文消息
查看>>
单点登录方案的比较和选择
查看>>
Android 涂鸦最佳实践
查看>>
Paste Deployment
查看>>
Ubuntu 解压错误
查看>>
eclipse项目(project)出现感叹号的一种处理办法
查看>>
CCSpawn 同步动作
查看>>
Gexmul虚拟机内存空间原理简述
查看>>