Scrapy框架: 使用cookies登录网站

import scrapy

class LoginSpider(scrapy.Spider):
    name = 'login'
    allowed_domains = ['xxx.com']
    start_urls = ['https://www.xxx.com/xx/']

    cookies = ""

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

    def parse(self, response):
        with open("01login.html", "wb") as f:
            f.write(response.body)

相关推荐