python爬虫入门篇:使用requests发送POST请求提交表单

一、定义

  • post()方法将携带某些数据的POST请求发送到指定的URL

二、应用场景

  1. 提交表单所涉及到的增删改操作。
  2. 调用API,例如百度云的文字识别接口、阿里云的常用支付接口,都需要用POST请求。
  3. 发送/上传图片、音视频等文件资源。

三、使用方法

1)导入模块

import requests

2)封装数据

将要发送的数据封装到data中,封装形式可以是字典、json、元组等。

# 发送字典
post_dict = {'key1': 'value1', 'key2': 'value2'}
# 发送元组
post_tuple = (('key1', 'value1'), ('key1', 'value2'))
# 发送json
post_json = json.dumps({'some': 'data'})
r1 = requests.post("http://httpbin.org/post", data=post_dict)
r2 = requests.post("http://httpbin.org/post", data=post_tuple)
r3 = requests.post("http://httpbin.org/post", data=post_json)

3)定制header头和cookie信息

cookie = "token=code_space;"
header = {
        "cookie": cookie,
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Connection": "keep-alive",
        "Content-Type": "application/json",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                      "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
}

四、测试demo

# -*- coding: utf-8 -*-
"""
@Time : 2022/1/18 11:36
@Auth : 技术空间
@File :post_demo.py
@IDE :PyCharm
@Motto:技术总是要日积月累的

"""

import requests
import json

if __name__ == '__main__':
    cookie = "token=code_space;"
    header = {
        "cookie": cookie,
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Connection": "keep-alive",
        "Content-Type": "application/json",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
    }
    # 发送字典
    post_dict = {'key1': 'value1', 'key2': 'value2'}
    # 发送元组
    post_tuple = (('key1', 'value1'), ('key1', 'value2'))
    # 发送json
    post_json = json.dumps({'some': 'data'})
    r1 = requests.post("http://httpbin.org/post", data=post_dict, headers=header, cookie=cookie)
    r2 = requests.post("http://httpbin.org/post", data=post_tuple, headers=header, cookie=cookie)
    r3 = requests.post("http://httpbin.org/post", data=post_json, headers=header, cookie=cookie)
    print("r1返回的内容为-->" + r1.text)
    print("r2返回的内容为-->" + r2.text)
    print("r3返回的内容为-->" + r3.text)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

五、其它拓展

  • 发送文件demo
# 定义file对象
files = {'file': open('demo.xls', 'rb')}
r = requests.post('http://httpbin.org/post', files=files)

到这里,我们在请求数据上常用的GET、POST方法已经学会了,在后续开发中可以以这些代码为****基础进行拓展。在爬虫的应用上,这些请求是核心基础。
关于爬虫的知识点和应用实践我将会在后续整理好笔记后更新上来。

  • 10
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Python中,可以使用requests库来发送POST请求提交表单数据。具体步骤如下: 1. 导入requests库:首先需要在Python代码中导入requests库,以便使用其中的POST方法。 2. 构造表单数据:使用字典或元组的形式构造表单数据,将需要提交的字段和对应的值添加到表单数据中。 3. 发送POST请求使用requests库的post方法发送POST请求,并将URL和表单数据作为参数传递给post方法。 4. 获取响应结果:通过调用响应对象的text属性,可以获取POST请求的响应结果。 下面是一个示例代码,通过POST请求提交表单数据: ```python import requests # 构造表单数据 data = { 'username': 'example', 'password': '123456' } # 发送POST请求 response = requests.post(url, data=data) # 获取响应结果 result = response.text ``` 其中,`url`是POST请求的目标URL,`data`是构造的表单数据。你可以根据实际情况修改`url`和`data`的值。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [python post请求_python发送post请求](https://blog.csdn.net/weixin_39848097/article/details/110401829)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [python request post from 提交表单](https://blog.csdn.net/brightgreat/article/details/126610046)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code_space

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值