首页IT科技require common sense(requests在python中如何发送请求)

require common sense(requests在python中如何发送请求)

时间2025-08-30 19:18:30分类IT科技浏览6403
导读:本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。...

本文教程操作环境:windows7系统              、Python 3.9.1              ,DELL G3电脑              。

1                        、get请求的部分参数

1) url(请求的url地址                        ,必需 )

importrequests url="http://www.baidu.com" resp=requests.get(url)#向url对应的服务器发送相应的get请求        ,获得对应的相应                        。

2)headers参数(请求头          ,可选)

importrequests url=r"https://www.baidu.com/s" Headers={"User-Agent":"Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/71.0.3578.98Safari/537.36" } response=requests.get(url=url,headers=Headers)

2        、requests.get请求实例

任何时候进行了类似 requests.get() 的调用                       ,你都在做两件主要的事情        。其一            ,你在构建一个 Request对象       , 该对象将被发送到某个服务器请求或查询一些资源          。其二                      ,一旦 requests 得到一个从服务器返回的响应就会产生一个 Response 对象                       。该响应对象包含服务器返回的所有信息                ,也包含你原来创建的 Request 对象            。如下是一个简单的请求    ,从 Wikipedia 的服务器得到一些非常重要的信息:

>>>r=requests.get(http://en.wikipedia.org/wiki/Monty_Python)

如果想访问服务器返回给我们的响应头部信息                      ,可以这样做:

>>>r.headers {content-length:56170,x-content-type-options:nosniff,x-cache: HITfromcp1006.eqiad.wmnet,MISSfromcp1010.eqiad.wmnet,content-encoding: gzip,age:3080,content-language:en,vary:Accept-Encoding,Cookie, server:Apache,last-modified:Wed,13Jun201201:33:50GMT, connection:close,cache-control:private,s-maxage=0,max-age=0, must-revalidate,date:Thu,14Jun201212:59:39GMT,content-type: text/html;charset=UTF-8,x-cache-lookup:HITfromcp1006.eqiad.wmnet:3128, MISSfromcp1010.eqiad.wmnet:80}

然而                    ,如果想得到发送到服务器的请求的头部,我们可以简单地访问该请求                  ,然后是该请求的头部:

>>>r.request.headers {Accept-Encoding:identity,deflate,compress,gzip, Accept:*/*,User-Agent:python-requests/0.13.1}

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
vue jsx双向绑定(前端随笔0:URL与状态的双向绑定) python 日志记录(python使用loguru操作日志)