scrapy之发送POST请求

可以使用 yield scrapy.FormRequest(url, formdata, callback)方法发送POST请求。
如果希望程序执行一开始就发送POST请求,可以重写Spider类的start_requests(self) 方法,并且不再调用start_urls里的url。
例如:

class mySpider(scrapy.Spider):
    # start_urls = ["http://www.baidu.com/"]

    def start_requests(self):
        url = 'http://www.renren.com/PLogin.do'

        # FormRequest 是Scrapy发送POST请求的方法
        yield scrapy.FormRequest(
            url = url,
            formdata = {"email" : "xxx", "password" : "xxxxx"},
            callback = self.parse_page
        )
    def parse_page(self, response):
        # do something

注意:表单数据的提交使用formdata,方法之间的传参依然使用meta,例如:

class mySpider(scrapy.Spider):
    # start_urls = ["http://www.example.com/"]

    def start_requests(self):
        url = 'http://www.renren.com/PLogin.do'

        # FormRequest 是Scrapy发送POST请求的方法
        yield scrapy.FormRequest(
            url = url,
            formdata = {"email" : "xxx", "password" : "xxxxx"},
            callback = self.parse_page,
        meta = {"key1":"value1","key2":"value2"}
        )
    def parse_page(self, response):
        value1 = response.meta["key1"]
        # do something

猜你喜欢

转载自blog.csdn.net/qq_37275405/article/details/81196234