Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Scrapy spider中获取管道对象_Python_Mongodb_Scrapy - Fatal编程技术网

Python 如何在Scrapy spider中获取管道对象

Python 如何在Scrapy spider中获取管道对象,python,mongodb,scrapy,Python,Mongodb,Scrapy,我已经使用了mongodb来存储爬网的数据 现在我想查询数据的最后日期,这样我就可以继续抓取数据,而不需要从url列表的开头重新启动它。(url,可以由日期确定,如:/2014-03-22.html) 我只需要一个连接对象来执行数据库操作,该操作正在进行中 因此,我想知道如何在spider中获取管道对象(不是新对象) 或者,任何更好的增量更新解决方案 提前谢谢 对不起,我的英语很差。。。 现在只需示例: # This is my Pipline class MongoDBPipeline(obj

我已经使用了mongodb来存储爬网的数据

现在我想查询数据的最后日期,这样我就可以继续抓取数据,而不需要从url列表的开头重新启动它。(url,可以由日期确定,如:/2014-03-22.html)

我只需要一个连接对象来执行数据库操作,该操作正在进行中

因此,我想知道如何在spider中获取管道对象(不是新对象)

或者,任何更好的增量更新解决方案

提前谢谢

对不起,我的英语很差。。。 现在只需示例:

# This is my Pipline
class MongoDBPipeline(object):
    def __init__(self, mongodb_db=None, mongodb_collection=None):
        self.connection = pymongo.Connection(settings['MONGODB_SERVER'], settings['MONGODB_PORT'])
        ....
    def process_item(self, item, spider):
        ....
    def get_date(self):
        ....
还有蜘蛛:

class Spider(Spider):
    name = "test"
    ....

    def parse(self, response):
        # Want to get the Pipeline object
        mongo = MongoDBPipeline() # if take this way, must a new Pipeline object
        mongo.get_date()          # In scrapy, it must have a Pipeline object for the spider
                                  # I want to get the Pipeline object, which created when scrapy started.
class Spider(Spider):
    name = "test"

    def __init__(self):
        self.myPipeline = None

    def parse(self, response):
        self.myPipeline.get_date()
好吧,只是不想换新东西……我承认我是强迫症患者。

根据《刮痧:

项目管道负责在项目完成后对其进行处理 已经被蜘蛛拔出(或刮去)

基本上,这意味着,首先,刮擦蜘蛛在工作,然后提取的项目将进入管道-没有办法倒退

一个可能的解决方案是,在管道本身中,检查您刮取的项目是否已经在数据库中

另一种解决方法是在数据库中保留已爬网的url列表,并在爬行器中检查是否已从url获取数据

因为我不知道你说的“从头开始”是什么意思,所以我不能提出任何具体的建议

希望这些信息至少对您有所帮助。

一个Scrapy管道有一个方法,该方法在spider初始化后执行。您可以将对数据库连接、get_date()方法或管道本身的引用传递给spider。后者与您的代码的示例如下:

# This is my Pipline
class MongoDBPipeline(object):
    def __init__(self, mongodb_db=None, mongodb_collection=None):
        self.connection = pymongo.Connection(settings['MONGODB_SERVER'], settings['MONGODB_PORT'])
        ....

    def process_item(self, item, spider):
        ....
    def get_date(self):
        ....

    def open_spider(self, spider):
        spider.myPipeline = self
然后,在蜘蛛中:

class Spider(Spider):
    name = "test"
    ....

    def parse(self, response):
        # Want to get the Pipeline object
        mongo = MongoDBPipeline() # if take this way, must a new Pipeline object
        mongo.get_date()          # In scrapy, it must have a Pipeline object for the spider
                                  # I want to get the Pipeline object, which created when scrapy started.
class Spider(Spider):
    name = "test"

    def __init__(self):
        self.myPipeline = None

    def parse(self, response):
        self.myPipeline.get_date()

我认为这里没有必要使用
\uuu init\uuu()
方法,但我把它放在这里是为了表明open\u spider在初始化后会替换它。

我的意思是,我抓取的网站url是按日期排序的,我可以根据日期确定需要抓取的url。保存url列表的方法是否可以获得管道对象?或者,需要一个新的数据库连接对象来检查它是否存在?@Pitty抱歉,我不明白你在问什么。你能举个小例子吗?谢谢。@Pitty是的,你可以在spider中实例化一个数据库连接-获取你已经抓取的url列表,然后在
parse
中检查
response.url
是否在列表中。这有意义吗?我知道我可以这样做,而且已经做到了,只是不知道如何在一个连接对象中建立连接…当运行spider时,scrapy必须初始化管道。在管道中,我创建了一个连接。我想知道,我是否可以获得连接(管道中的init),而不是新的连接。这意味着在爬网过程中,只创建了一个连接,而不是两个。。