脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python 包 requests 实现请求操作

python 包 requests 实现请求操作

2022-11-21 11:19autofelix Python

这篇文章主要介绍了python 包 requests 实现请求操作,文章介绍内容包括带参数请求、自定义headers,文章内容详细具有一定的参考价值,需要的小伙伴可以参考一下

一、安装

?
1
pip install requests

二、请求类型

?
1
2
3
4
5
6
7
8
import requests
 
requests.get('https://www.baidu.com')
requests.post('https://www.baidu.com')
requests.put('https://www.baidu.com')
requests.delete('https://www.baidu.com')
requests.head('https://www.baidu.com')
requests.options('https://www.baidu.com')

三、带参数请求

?
1
2
3
4
5
6
7
8
9
10
import requests
 
data = {
'name': 'autofelix',
'age': 25
}
 
response = requests.get('https://www.baidu.com', params=data)
print(response.url)
print(response.text)

四、自定义headers

?
1
2
3
4
5
6
7
import requests
 
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'
}
response =requests.get('https://www.baidu.com', headers=headers)
print(response.text)

五、请求属性

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import requests
 
response = requests.get('https://www.baidu.com')
 
# 响应状态码
response.status_code
# 响应头
response.headers
# 响应cookie
response.cookies
# 请求url
response.url
# 历史记录
response.history

六、文件上传

?
1
2
3
4
import requests
 
files= {"files":open("git.jpeg","rb")}
response = requests.post('https://www.baidu.com/upload', files=files)

七、会话维持

?
1
2
3
4
5
import requests
 
s = requests.Session()
s.get('https://www.baidu.com/login')
response = s.get('https://www.baidu.com')

八、证书验证

  • verify设置False关闭证书验证
  • urllib3可以解决InsecureRequestWarning提示
?
1
2
3
4
5
import requests
from requests.packages import urllib3
 
urllib3.disable_warnings()
response = requests.get('https://www.12306.cn', verify=False)

九、代理设置

?
1
2
3
4
5
6
7
import requests
 
proxies= {
'http': 'http://127.0.0.1:9999',
'https': 'http://127.0.0.1:8888'
}
response = requests.get('https://www.baidu.com', proxies=proxies)

十、超时设置

?
1
2
3
import requests
 
requests.get('https://www.baidu.com', timeout=3)

十一、认证设置

?
1
2
3
import requests
 
response = requests.get('https://www.baidu.com', auth=('user', '123'))

十二、异常处理

  • ReadTimeout是超时错误
  • ConnectionError是网络错误
?
1
2
3
4
5
6
7
8
9
10
11
12
import requests
from requests.exceptions import ReadTimeout,ConnectionError,RequestException
 
try:
response = requests.get('https://www.baidu.com', timout=0.1)
print(response.status_code)
except ReadTimeout:
print('timeout')
except ConnectionError:
print('connection Error')
except RequestException:
print('error')

到此这篇关于python 包 requests 实现请求操作的文章就介绍到这了,更多相关python  equests 请求操作内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.51cto.com/autofelix/5152306

延伸 · 阅读

精彩推荐
  • Pythonpython实现水仙花数实例讲解

    python实现水仙花数实例讲解

    这篇文章主要介绍了python实现水仙花数实例讲解,有正在学习python的同学可以跟着小编一起来学习下水仙花数怎么用python计算吧...

    看,月亮在跳舞6382021-09-26
  • Python基于Python实现文件大小输出

    基于Python实现文件大小输出

    在数据库中存储时,使用 Bytes 更精确,可扩展性和灵活性都很高。下面通过本文给大家介绍基于Python实现文件大小输出,对python文件输出相关知识感兴趣的...

    jerrylsxu4232020-08-06
  • Python用Python编写一个基于终端的实现翻译的脚本

    用Python编写一个基于终端的实现翻译的脚本

    这篇文章主要介绍了用Python编写一个基于终端的实现翻译的脚本,代码基于Python2.x,需要的朋友可以参考下 ...

    C Wong1952020-06-12
  • Pythonpython OpenCV实现答题卡识别判卷

    python OpenCV实现答题卡识别判卷

    这篇文章主要为大家详细介绍了python OpenCV实现答题卡识别判卷,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    乐亦亦乐9642021-12-07
  • Pythonpython生成随机mac地址的方法

    python生成随机mac地址的方法

    这篇文章主要介绍了python生成随机mac地址的方法,涉及Python操作随机字符串的技巧,需要的朋友可以参考下 ...

    上大王7052019-12-03
  • Pythonpip安装Python库时遇到的问题及解决方法

    pip安装Python库时遇到的问题及解决方法

    这篇文章主要介绍了pip安装Python库时遇到的问题及解决方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下...

    david_huang6282020-12-20
  • PythonPython实现九宫格式的朋友圈功能内附“马云”朋友圈

    Python实现九宫格式的朋友圈功能内附“马云”朋友圈

    PIL(Python Imaging Library)是一个非常强大的Python库,但是它支持Python2.X, 在Python3中则使用的是Pillow库,它是从PIL中fork出来的一个分支。这篇文章主要介绍了用...

    初一丶5892021-06-24
  • PythonPytorch如何切换 cpu和gpu的使用详解

    Pytorch如何切换 cpu和gpu的使用详解

    这篇文章主要介绍了Pytorch如何切换 cpu和gpu的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

    遇到好事了10582021-09-12