Python InitSpider存在scrapy登录问题

Python InitSpider存在scrapy登录问题,python,scrapy,Python,Scrapy,我正在尝试使用InitSpider进行身份验证的scrapy登录。由于某种原因,使用InitSpider它总是无法登录。我的代码与下面帖子中的答案类似: 我在日志中看到的响应是: 2012-12-20 22:56:53-0500 [linked] DEBUG: Redirecting (302) to <GET https://example.com/> from <POST https://example.com/> 以上是我的spider代码。经过一段时间的努力,

我正在尝试使用
InitSpider
进行身份验证的scrapy登录。由于某种原因,使用
InitSpider
它总是无法登录。我的代码与下面帖子中的答案类似:

我在日志中看到的响应是:

2012-12-20 22:56:53-0500 [linked] DEBUG: Redirecting (302) to <GET https://example.com/> from <POST https://example.com/>

以上是我的spider代码。

经过一段时间的努力,我找到了答案,并在我的博客上发布了解决方案:


基本上,我使用
BaseSpider
并覆盖
start\u requests
方法来处理登录。

粘贴spider类的完整代码刚刚添加了代码。当我遇到此类问题时,我会使用wireshark调试HTTP请求和响应。您使用的是哪种版本的scrapy?
class DemoSpider(InitSpider):
    name = 'linked'
    login_page = # Login URL
    start_urls = # All other urls

    def init_request(self):
        #"""This function is called before crawling starts."""
        return Request(url=self.login_page, callback=self.login)

    def login(self, response):
        #"""Generate a login request."""
        return FormRequest.from_response(response, 
            formdata={'username': 'username', 'password': 'password'},
            callback=self.check_login_response)

    def check_login_response(self, response):
        #"""Check the response returned by a login request to see if we are successfully logged in."""
        if "Sign Out" in response.body:
            self.log("\n\n\nSuccessfully logged in. Let's start crawling!\n\n\n")
            # Now the crawling can begin..
            return self.initialized()
        else:
            self.log("\n\n\nFailed, Bad times :(\n\n\n")
            # Something went wrong, we couldn't log in, so nothing happens.

    def parse(self, response):
        self.log('got to the parse function')