Loading

Scrapy的post请求

scrapy默认发送的是get请求,发送post请求时需要重写start_requests(self)方法

import scrapy


class PostspiderSpider(scrapy.Spider):
    name = 'postSpider'
    allowed_domains = ['www.xxx.com']
    start_urls = ['http://www.xxx.com/']

    def start_requests(self):
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
        }
        data = {
            "name": "tom",
            "age": "82"
        }
        for url in self.start_urls:
            yield scrapy.FormRequest(url=url, headers=headers, formdata=data, callback=self.parse)

    def parse(self, response):
        pass
posted @ 2021-11-01 18:56  北兢王  阅读(475)  评论(0)    收藏  举报