Python 官方文档:入门教程 => 点击学习
目录一、安装二、请求类型三、带参数请求四、自定义headers五、请求属性六、文件上传七、会话维持八、证书验证九、代理设置十、超时设置十一、认证设置十二、异常处理一、安装 pip i
pip install requests
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')
import requests
data = {
'name': 'autofelix',
'age': 25
}
response = requests.get('https://www.baidu.com', params=data)
print(response.url)
print(response.text)
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)
import requests
response = requests.get('https://www.baidu.com')
# 响应状态码
response.status_code
# 响应头
response.headers
# 响应cookie
response.cookies
# 请求url
response.url
# 历史记录
response.history
import requests
files= {"files":open("git.jpeg","rb")}
response = requests.post('https://www.baidu.com/upload', files=files)
import requests
s = requests.Session()
s.get('https://www.baidu.com/login')
response = s.get('https://www.baidu.com')
verify
设置False关闭证书验证InsecureRequestWarning
提示import requests
from requests.packages import urllib3
urllib3.disable_warnings()
response = requests.get('https://www.12306.cn', verify=False)
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)
import requests
requests.get('https://www.baidu.com', timeout=3)
import requests
response = requests.get('https://www.baidu.com', auth=('user', '123'))
ReadTimeout
是超时错误ConnectionError
是网络错误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 请求操作内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: python 包 requests 实现请求操作
本文链接: https://lsjlt.com/news/144905.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0