import requests

print(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 # 参数json json = {
"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 HTTPProxyAuth
proxies = {
"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 HTTPBasicAuth
from requests.auth import HTTPDigestAuth l5 = 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 closing
with closing(requests.get("http://ddddddd",stream=True)) as f:
for i in f.iter_content():
print(i) # cert,证书参数,告诉request去这个地方去下载cert
l8 = 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 requests
session = 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")

python的requests模块参数详解的更多相关文章

  1. python datetime模块参数详解

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块,它提供 的接口与C标准库time.h基本一致.相比于time模块,datetime模块的接 ...

  2. 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码

    本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...

  3. python os.path模块常用方法详解

    os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法.更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.ht ...

  4. python os.path模块常用方法详解(转)

    转自:https://www.cnblogs.com/wuxie1989/p/5623435.html os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法.更多的方 ...

  5. python os.path模块常用方法详解 ZZ

    os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法.更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.ht ...

  6. python中 datetime模块的详解(转载)

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...

  7. Python中标准模块importlib详解

    1 模块简介 Python提供了importlib包作为标准库的一部分.目的就是提供Python中import语句的实现(以及__import__函数).另外,importlib允许程序员创建他们自定 ...

  8. python os.path模块用法详解

    abspath 返回一个目录的绝对路径 Return an absolute path. >>> os.path.abspath("/etc/sysconfig/selin ...

  9. python os.path模块常用方法详解:转:http://wangwei007.blog.51cto.com/68019/1104940

    1.os.path.abspath(path) 返回path规范化的绝对路径. >>> os.path.abspath('test.csv') 'C:\\Python25\\test ...

随机推荐

  1. List<Map<String, Object>>集合中获取某个key并转换为List<Integer>集合

    package com.codyy.sso.controller.yuanqu; import java.util.ArrayList; import java.util.HashMap; impor ...

  2. bootstrap-treeview 如何实现全选父节点下所有子节点及反选

    转(https://www.augsky.com/992.html) 选中父节点时,父节点下所有子节点也都全部选中 1,HTML代码 <h2>TreeView Checkable</ ...

  3. 数据传输流程和socket简单操作

    一.***C/S架构:客户端(client)/服务端(server)架构, B/S架构:浏览器(browser) / 服务端(server)架构 软件cs架构:浏览器,qq,微信,陌陌等等硬件cs架构 ...

  4. PyQt5实现邮件合并功能(GUI)

    1. 实战Word批量 需要处理批量替换word的一些数据,数据源从Excel中来. Excel的百分数会变为数字,以及浮点数会多好多精度,为了原汁原味的数据,直接复制数据到文本文件.通过\t来分隔即 ...

  5. 前端 js加密 后台java 解密 RSA

    前端代码 : $.ajax({ type:"GET", url:"http://localhost:8084/getPulbicKey", dataType:& ...

  6. Java2E中的路径问题

    本节主要介绍: 1.request.getContextPath()-----项目的发布的根路径 2.request.getRealPath('t')----t目录在当前磁盘中的物理位置,包括盘符,文 ...

  7. Eclipse识别不了jsp中的${pageContxt.request.contextPath }

    按计划这周系统学下Struts,但是搭完框架后jsp页面识别不了${pageContxt.request.contextPath } So这篇就记录一下我是怎么解决这个问题的 不管是tomcat7.0 ...

  8. SpringCloud系列十:SpringCloudConfig 高级配置(密钥加密处理(JCE)、KeyStore 加密处理、SpringCloudConfig 高可用机制、SpringCloudBus 服务总线)

    1.概念:SpringCloudConfig 高级配置 2.具体内容 在 SpringCloudConfig 之中考虑到所有配置文件都暴露在远程仓库之中的安全性问题,所以提供有安全访问的处理机制,这样 ...

  9. Shell 批量修改主机 用户密码

    问题:132.121.114 和 132.121.118 网段共 48 台主机未添加基础监控,但是 wh 账户不能登录 需进行批量修改密码操作. 目前情况:op1对上述48台机器设备均能免密登录. 操 ...

  10. 侧脸生成正脸概论与精析(一)Global and Local Perception GAN

    侧脸生成正脸我一直很感兴趣,老早就想把这块理一理的.今天来给大家分享一篇去年的老文章,如果有不对的地方,请斧正. Beyond Face Rotation: Global and Local Perc ...