requests在python中如何发送请求
requests在python中如何发送请求
本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。
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'}
以上就是requests在python中发送请求的方法,大家对于基础的get参数有所了解后,可以自己动手试着给服务器发送请求,看看能否独立完成相关的操作。更多Python学习教程请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
Python如何在画布中绘制多个图表?
python如何在画布中绘制多个图表?1、说明在绘制图表时,Matplotlib模块默认先创建一块画布,然后将绘制的图表显示在画布上。若要在一个画布上画...详情>>
2023-11-14 21:04:27
python@是什么意思
python中@是什么意思通常我们所见的@符号是用于邮箱中,而在python中@符号也有着重要的作用,一个是表示修饰符,另一个则是表示矩阵乘法。表示...详情>>
2023-11-14 20:07:01
python进程池的使用注意
python进程池的使用注意1、使用注意(1)使用池时,如果未指定进程数,则默认为CPU核心数。(2)核心数量对应于计算机的逻辑处理器数量(任务管理器-...详情>>
2023-11-14 18:42:08
python多线程中join的应用场景
python多线程中join的应用场景1、线程的join()方法表示等这个线程运行完毕,程序再往下运行。我们来看下面的例子fromthreadingimportThreadt=ti...详情>>
2023-11-14 18:16:56热门推荐
python中scipy.linalg.inv()功能是什么?
沸requests在python中如何发送请求
热python自定义模块的注意点
热python如何将相对路径转换为绝对路径?
新python在列表中和列表末尾添加元素的方法
Python如何在画布中绘制多个图表?
pythonQueue在python的实例化
python@是什么意思
pythonconftest如何使用?
python列表运算详解
python进程池的使用注意
python多线程中join的应用场景
python异步中loop抛出异常的解决
pythontime模块处理系统时间的函数
技术干货






