1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | 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格式,但是数据有中文呢就额可以使用下面的方式来发送数据
1 2 3 4 | data = bytes(json.dumps( data_dict, ensure_ascii = False ),encoding = "utf-8" ) |
有的时候参数是一个列表,此时我们该怎么传递参数呢?其实我们也可以用json来传参
1 2 3 4 5 6 7 8 9 10 | res = requests.request( method = "delete" , url = delurl, headers = { "Authorization" : Authorization, "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36" , "Content-Type" : "application/json;charset=UTF-8" }, json = [bid,] ) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本