Requests库
Requests 是使用Apache2 Licensed 许可证的HTTP 库。用Python 编写。
Requests 使用的是urllib3,因此继承了它的所有特性。Requests 支持HTTP 连接保持和连接池,支持使用cookie 保持会话,支持文件上传,支持自动确定响应内容的编码,支持国际化的URL 和POST 数据自动编码。
1.Requests安装
下载地址:https://pypi.python.org/pypi/requests/
官方网站:http://docs.python-requests.org/en/master/
中文文档:http://cn.python-requests.org/zh_CN/latest/
2.Demo
>>> r = requests.get('https://api.github.com/user', auth=('user', 'psw')) >>> r.status_code 200 >>> r.headers['content-type'] 'application/json; charset=utf8' >>> r.encoding 'utf-8' >>> r.text u'{"type":"User"...' >>> r.json() {u'private_gists': 419, u'total_private_repos': 77, ...}
其中auth=('user', 'psw')为github系统注册的账号和密码
3.Post请求模拟登录
import requests url1 = 'http://www.exanple.com/login'#登录地址 url2 = "http://www.example.com/main"#需要登录才能访问的地址 data={"user":"user","password":"pass"} headers = { "Accept":"text/html,application/xhtml+xml,application/xml;", "Accept-Encoding":"gzip", "Accept-Language":"zh-CN,zh;q=0.8", "Referer":"http://www.example.com/", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" } res1 = requests.post(url1, data=data, headers=headers) res2 = requests.get(url2, cookies=res1.cookies, headers=headers) print(res2.content)#获得二进制响应内容 print(res2.raw)#获得原始响应内容,需要stream=True print(res2.raw.read(50)) print(type(res2.text))#返回解码成unicode的内容 print(res2.url) print(res2.history)#追踪重定向 print(res2.cookies) print(res2.cookies['example_cookie_name']) print(res2.headers) print(res2.headers['Content-Type']) print(res2.headers.get('content-type')) print(res2.json)#讲返回内容编码为json print(res2.encoding)#返回内容编码 print(res2.status_code)#返回http状态码 print(res2.raise_for_status())#返回错误状态码
4.使用Session()对象的写法
import requests s = requests.Session() url1 = 'http://www.exanple.com/login'#登录地址 url2 = "http://www.example.com/main"#需要登录才能访问的页面地址 data={"user":"user","password":"pass"} headers = { "Accept":"text/html,application/xhtml+xml,application/xml;", "Accept-Encoding":"gzip", "Accept-Language":"zh-CN,zh;q=0.8", "Referer":"http://www.example.com/", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" } res1 = s.post(url1, data=data) res2 = s.post(url2) print(res2.content)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!