request.post
requests.post
requests.post(url, data=None, json=None, **kwargs)
参数 | 类型 | 描述 |
---|---|---|
url | 字符串 | 请求的网页,对应的是form的action属性的值,而不是form所在的页面 |
data | form提交的数据 | key是form的name属性对应的值,value是所填入的值,通过chrome审查元素(inspector) |
json | json格式的数据 | 注意看请求过程中是否出现Content-Type: application/json,json参数其实是data的一个快捷方式,在data参数中传入json.dumps(dict)是同一个效果 |
**kwargs | 其他的参数 | 比如headers |
1.表单请求
1.from表单
<form method="post" action="processing.php">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname"><br>
<input type="submit" value="Submit">
</form>
2.request请求
import requests
params = {'firstname': 'Ryan', 'lastname': 'Mitchell'}
r = requests.post("http://pythonscraping.com/files/processing.php", data=params)
2.提交文件
2.1.form
<form action="processing2.php" method="post" enctype="multipart/form-data">
Submit a jpg, png, or gif: <input type="file" name="uploadFile"><br>
<input type="submit" value="Upload File">
</form>
2.2.python
import requests
files = {'uploadFile': open('../files/Python-logo.png', 'rb')} #这里提交的python文件对象(字节)
r = requests.post("http://pythonscraping.com/pages/processing2.php",
files=files)
3.处理cookie
3.1.人工方法获得cookie
import requests
params = {'username': 'Ryan', 'password': 'password'}
r = requests.post("http://pythonscraping.com/pages/cookies/welcome.php", params) # 第一次请求获得的cookie
print("Cookie is set to:")
print(r.cookies.get_dict())
r = requests.get("http://pythonscraping.com/pages/cookies/profile.php",
cookies=r.cookies) # 用上一次的请求所获得的cookie
print(r.text)
3.2.session回话保存cookie
import requests
session = requests.Session() # 新建一个回话,他会保存每次访问的cookie,header等HTTP协议信息,不必每次手动提交cookie
params = {'username': 'username', 'password': 'password'}
s = session.post("http://pythonscraping.com/pages/cookies/welcome.php", params)
print("Cookie is set to:")
print(s.cookies.get_dict())print("Going to profile page...")
s = session.get("http://pythonscraping.com/pages/cookies/profile.php")
print(s.text)
参考文献:
python网络数据采集
request.post相关推荐
- 端口映射问题:Bad Request This combination of host and port requires TLS.
错误信息: Bad Request This combination of host and port requires TLS. 遇到上面的错误信息:如果是通过域名访问,则该域名后配置的转发端口映射 ...
- GitHub 中 Merge pull request 的 3 中选项说明
Merge pull request 提供了 3 种 merge 方法: Create a merge commit:GitHub 的底层操作是 git merge --no-ff.feature 分 ...
- 将request中的所有参数存放到自定义的map中
/*** <获取参数map>* * @return 参数map* @throws Exception */protected Map<String, Object> getPa ...
- Django Request对象3.3
HttpReqeust对象 服务器接收到http协议的请求后,会根据报文创建HttpRequest对象 视图函数的第一个参数是HttpRequest对象 在django.http模块中定义了HttpR ...
- 微信 request 合法域名校验出错
报错信息 at Feb 19 2022 10:42:34 GMT+0800 (中国标准时间) request 合法域名校验出错如若已在管理后台更新域名配置,请刷新项目配置后重新编译项目,操作路径:&q ...
- 微信小程序 在使用wx.request时显示加载中
我们可以用wx.showLoading(OBJECT),当请求服务器的地方多了,怎么才能不每次都要去调用函数,我们只要对wx.request加工下就可以了,在utils下新建js文件network.j ...
- 微信小程序wx.request POST获取不到数据解决办法
get //发起请求 wx.request({ url: 'http://www.xiaochengxu.com/home/index/curd', //仅为示例,并非真实的接口地 ...
- 织梦手机站下一篇变上一篇而且还出错Request Error!
最新的织梦dedecms程序手机版下一篇变上一篇而且还出错Request Error!,这是因为官方写错了一个地方 打开 /include/arc.archives.class.php 找到 $mli ...
- 3.request response
rr的介绍 request是代表请求,response是响应 执行流程: 1.浏览器发送请求 2.服务器接受请求,创建两个对象,将请求信息封装给request,将两个对象发送给对应的servlet 3 ...
- request 对象和 response 对象
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象 HttpServletResponse HttpServletR ...
最新文章
- php中类和对象的操作
- C++中构造函数和析构函数可以抛出异常吗?
- No PIL installation found INFO:superset.utils.screenshots:No PIL installation found
- 中国药妆行业投资现状与发展策略分析报告2022-2028年
- Ionic+Angular+Express实现前后端交互使用HttpClient发送get请求数据并加载显示(附代码下载)
- idea从gitlab拉项目到本地_Git入门-基础命令,用github免费服务器域名部署小项目...
- CF374 Maxim and Array
- 面试中经常会问的智力题,来看看你会做几道
- maven私有库配置
- vue项目打包:npm run build 进程卡死
- 电学 —— 库仑定律
- 由《资深架构师带你玩转框架封装》课程——我的实践
- java 打印 xps_使用Java 将PPT转换为PDF、XPS等格式
- java代码处理URL转码
- RGB颜色值与十六进制颜色码转换及颜色码对照表
- 阅读笔记:利用Python进行数据分析第2版——第10章 数据聚合与分组运算
- Android开发技术周报 Issue#8
- 金蝶K3--生产领料单--更新销售订单编号
- latex 公式换行_如何在微信公众号中!编辑出漂亮的数学公式?
- 北大齐教授健康讲座笔录
热门文章
- VUE---不连接后端的登录验证规则方法
- pythonindexerror_Python index错误值不在list…on.index(value)
- Python+numpy实现矩阵QR分解
- C++ const对成员函数的修饰 及 取地址及const取地址操作符重载
- python paramiko并发_Python之paramiko
- QT每日一练day16:QMessageBox消息对话框
- C++ vector使用示例
- php动态交叉表,PHP Array交叉表实现代码
- php获取每月的星期天,php计算一月中有多少个周末
- svn怎么检出两个地址项目_svn 检出项目报错