Scrapy 发送 post 请求

发送 post 请求,使用 scrapy.FormRequest 方法

# 发送post请求
yield scrapy.FormRequest(
    # url
    url=post_url,
    # post接口参数
    formdata=post_data,
    # 请求头
    headers=self.headers,
    # cookie
    cookies=self.cookies,
    # 回调方法
    callback=self.after_reply
)

示例:回复贴吧信息

import scrapy


class TeiBaSpider(scrapy.Spider):
    name = "teiba"
    allowed_domains = ["tieba.baidu.com"]
    start_urls = ["https://tieba.baidu.com/p/91xxxxxxx"]

    cookies_str = 'XFI=720862e0-5c86-11ef-90cf-edada47cd573; XFCS=A534DA9021023FD1141B9DA6E929053811C874F; XFT=nZmiPbxW66wPyRLQUCjslM1POx8BU3zu/EpEbunMvbA=; BIDUPSID=CC4827F65EB646623A40EA9'

    cookies = {data.split('=')[0]: data.split('=')[-1] for data in cookies_str.split("; ")}

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        'Host': 'tieba.baidu.com'
    }

    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(url, headers=self.headers, callback=self.parse, cookies=self.cookies)

    def parse(self, response):
        # 添加评论的post url
        post_url = "https://tieba.baidu.com/f/commit/post/add"

        # post 请求参数
        post_data = {
            "content": "我是回复内容123456",
            "ie": "utf-8",
            "kw": "沙井",
            "fid": "335358",
            "floor_num": "38",
            "rich_text": "1",
            "basilisk": "1",
            "mouse_pwd_isclick": "1",
            "ev": "comment",
            "__type__": "reply",
            "geetest_success": "0"
        }

        # 发送post请求
        yield scrapy.FormRequest(
            # url
            url=post_url,
            # post接口参数
            formdata=post_data,
            # 请求头
            headers=self.headers,
            # cookie
            cookies=self.cookies,
            # 回调方法
            callback=self.after_reply
        )

    def after_reply(self, response):
        print(response.status)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

又逢乱世

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

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

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

打赏作者

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

抵扣说明:

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

余额充值