Cookies 恢复爬网后无法再次登录。饼干在恢复粘性后不会变粘

Cookies 恢复爬网后无法再次登录。饼干在恢复粘性后不会变粘,cookies,scrapy,Cookies,Scrapy,我有一只爬行蜘蛛,代码如下。我通过TSock使用Tor。 当我启动spider时,一切正常。使用init_请求,我可以登录站点并使用粘性cookie进行爬网 但问题发生在我停下来继续蜘蛛的时候。饼干变得不粘了 我给你Scrapy的回复 =======================INIT_REQUEST================ 2013-01-30 03:03:58+0300 [my] INFO: Spider opened 2013-01-30 03:03:58+0300 [my]

我有一只爬行蜘蛛,代码如下。我通过TSock使用Tor。 当我启动spider时,一切正常。使用init_请求,我可以登录站点并使用粘性cookie进行爬网

但问题发生在我停下来继续蜘蛛的时候。饼干变得不粘了

我给你Scrapy的回复

=======================INIT_REQUEST================
2013-01-30 03:03:58+0300 [my] INFO: Spider opened
2013-01-30 03:03:58+0300 [my] INFO: Resuming crawl (675 requests scheduled)
............ And here crawling began
所以。。。回调=def init_请求中的self.login_url未被激发

我认为scrapy引擎不想在登录页面上再次发送请求。在恢复scrapy之前,我将登录页面(我可以从站点上的每个页面登录)更改为限制XPath中未包含的其他页面

结果是-恢复后,我无法登录,以前的cookie丢失

有人有一些假设吗

from scrapy.contrib.spiders import CrawlSpider,Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.loader import XPathItemLoader
from scrapy.contrib.loader.processor import TakeFirst, MapCompose, Join, Identity
from beles_com_ua.items import Product
from scrapy.utils.response import get_base_url
from scrapy.utils.url import urljoin_rfc
from scrapy.utils.markup import remove_entities
from django.utils.html import strip_tags
from datetime import datetime
from scrapy import log
import re
from scrapy.http import Request, FormRequest

class ProductLoader(XPathItemLoader):
    .... some code is here ...


class MySpider(CrawlSpider):
    name = 'my'
    login_page = 'http://test.com/index.php?section=6&type=12'

    allowed_domains = ['test.com']
    start_urls = [
        'http://test.com/index.php?section=142',
    ]
    rules = (
        Rule(SgmlLinkExtractor(allow=('.',),restrict_xpaths=('...my xpath...')),callback='parse_item', follow=True),
    )
    def start_requests(self):
        return self.init_request()

    def init_request(self):
        print '=======================INIT_REQUEST================'
        return [Request(self.login_page, callback=self.login_url)]


    def login_url(self, response):
        print '=======================LOGIN======================='
        """Generate a login request."""
        return FormRequest.from_response(response,
            formdata={'login': 'mylogin', 'pswd': 'mypass'},
            callback=self.after_login)

    def after_login(self, response):
        print '=======================AFTER_LOGIN ...======================='
        if "images/info_enter.png" in response.body:
               print "==============Bad times :(==============="
        else:
           print "=========Successfully logged in.========="
           for url in self.start_urls:
            yield self.make_requests_from_url(url)

    def parse_item(self, response):
        hxs = HtmlXPathSelector(response)

        entry = hxs.select("//div[@class='price']/text()").extract()
        l = ProductLoader(Product(), hxs)
        if entry:
        name = hxs.select("//div[@class='header_box']/text()").extract()[0]
        l.add_value('name', name)
        ... some code is here ...
        return l.load_item()
init\u请求(self):
仅当您从
InitSpider
not
CrawlSpider

您需要像这样从InitSpider中将您的spider子类化

class WorkingSpider(InitSpider):

    login_page = 'http://www.example.org/login.php'
    def init_request(self):
        #"""This function is called before crawling starts."""
        return Request(url=self.login_page, callback=self.login)
但是请记住,您不能在
initSpider
中定义
规则,因为它只在
CrawlSpider
中可用。您需要手动提取链接只有在从
initSpider
中创建子类时,
init_请求(self):
才可用

您需要像这样从InitSpider中将您的spider子类化

class WorkingSpider(InitSpider):

    login_page = 'http://www.example.org/login.php'
    def init_request(self):
        #"""This function is called before crawling starts."""
        return Request(url=self.login_page, callback=self.login)

但是请记住,您不能在
initSpider
中定义
规则
,因为它只能在
CrawlSpider
中使用。您需要手动提取链接

我不知道为什么,但我的代码可以很好地处理init_请求。只有在我尝试resume spider时,它才会失败。您的代码正在工作,因为当您启动spider时,您正在手动调用
start\u URL
中的
self.init\u request()
,当您只启动一次爬虫程序时会调用该函数。因此,当您停止并继续时,不会再次调用
start\u url
ia,也不会调用您的
init\u请求。使用您当前的代码,您可以使用任何名称,而不是
init_request
,因为eu正在手动调用,所以名称并不重要,但从自动调用它开始,您需要从init_spider中创建子类(非常感谢您的最佳解释))我不知道为什么,但我的代码与init_请求配合得很好。只有在我尝试resume spider时,它才会失败。您的代码正在工作,因为当您启动spider时,您正在手动调用
start\u URL
中的
self.init\u request()
,当您只启动一次爬虫程序时会调用该函数。因此,当您停止并继续时,不会再次调用
start\u url
ia,也不会调用您的
init\u请求。使用当前代码,您可以使用任何名称,而不是
init_request
,因为eu正在手动调用,所以名称并不重要,但从自动调用它开始,您需要从init_spiders中创建子类(非常感谢您的最佳解释))