requests在python中发送请求的实例讲解
作者:小妮浅浅 时间:2022-06-26 09:04:07
当我们想给服务器发送一些请求时,可以选择requests库来实现。相较于其它库而言,这种库的使用还是非常适合新手使用的。本篇要讲的是requests.get请求方法,这里需要先对get请求时的一些参数进行学习,在掌握了基本的用法后,可以就下面的requests.get请求实例进一步的探究。
1、get请求的部分参数
(1) url(请求的url地址,必需 )
import requests
url="http://www.baidu.com"
resp=requests.get(url)#向url对应的服务器发送相应的get请求,获得对应的相应 。
(2)headers参数 请求头,可选
import requests
url=r"https://www.baidu.com/s"
Headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/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':
'HIT from cp1006.eqiad.wmnet, MISS from cp1010.eqiad.wmnet', 'content-encoding':
'gzip', 'age': '3080', 'content-language': 'en', 'vary': 'Accept-Encoding,Cookie',
'server': 'Apache', 'last-modified': 'Wed, 13 Jun 2012 01:33:50 GMT',
'connection': 'close', 'cache-control': 'private, s-maxage=0, max-age=0,
must-revalidate', 'date': 'Thu, 14 Jun 2012 12:59:39 GMT', 'content-type':
'text/html; charset=UTF-8', 'x-cache-lookup': 'HIT from cp1006.eqiad.wmnet:3128,
MISS from cp1010.eqiad.wmnet:80'}
然而,如果想得到发送到服务器的请求的头部,我们可以简单地访问该请求,然后是该请求的头部:
>>> r.request.headers
{'Accept-Encoding': 'identity, deflate, compress, gzip',
'Accept': '*/*', 'User-Agent': 'python-requests/0.13.1'}
内容扩展:
发送get请求
# 导入requests模块
import requests
# 接口地址
url = 'http://v.juhe.cn/historyWeather/citys'
# 请求的参数数据
da = {'key':'61e0c8a6d9614382afbaaf35dbd3ec6','province_id':'4'}
# 发送请求
r = requests.get(url,params=da)
# 获取返回的json
js = r.json()
print(js)
print(js['resultcode'])
print(js['reason'])
print(js['result'])
print(js['error_code'])
来源:https://www.py.cn/jishu/jichu/23805.html
标签:requests,python,发送请求
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python进度条显示之tqmd模块
2021-09-29 22:44:36
![](https://img.aspxhome.com/file/2023/9/85179_0s.png)
python装饰器相当于函数的调用方式
2021-05-13 13:39:03
python实现对excel进行数据剔除操作实例
2022-09-28 13:53:22
python实现npy格式文件转换为txt文件操作
2021-04-03 08:07:05
![](https://img.aspxhome.com/file/2023/5/92075_0s.jpg)
Facebook开源一站式服务python时序利器Kats详解
2023-11-13 18:29:13
![](https://img.aspxhome.com/file/2023/7/62877_0s.png)
编程活动中几个不良现象
2008-09-01 12:23:00
GO语言原生实现文件上传功能
2023-10-17 08:29:39
![](https://img.aspxhome.com/file/2023/2/103032_0s.jpg)
Python使用docx模块处理word文档流程详解
2023-03-08 15:45:06
![](https://img.aspxhome.com/file/2023/4/90704_0s.png)
python实现AES算法及AES-CFB8加解密源码
2022-06-14 01:24:01
python snownlp情感分析简易demo(分享)
2021-07-18 04:32:35
![](https://img.aspxhome.com/file/2023/0/72250_0s.jpg)
python3.0 模拟用户登录,三次错误锁定的实例
2022-07-23 01:35:48
php验证session无效的解决方法
2023-08-15 06:52:39
详解如何用Python实现感知器算法
2023-11-02 13:32:20
![](https://img.aspxhome.com/file/2023/3/91973_0s.png)
python pands实现execl转csv 并修改csv指定列的方法
2022-11-20 01:45:27
Python编程把二叉树打印成多行代码
2023-06-24 15:12:57
python如何获得list或numpy数组中最大元素对应的索引
2021-02-10 11:30:12
Python实现视频中添加音频工具详解
2022-06-03 12:32:28
python中实现精确的浮点数运算详解
2022-12-27 07:08:27
python实现KNN分类算法
2023-03-01 07:53:36
![](https://img.aspxhome.com/file/2023/2/90702_0s.jpg)
python 限制函数执行时间,自己实现timeout的实例
2023-08-03 00:49:12