【问题标题】:How to store cumulated data after Scrapy has finished working with every URL?Scrapy 处理完每个 URL 后如何存储累积数据?
【发布时间】:2014-04-23 22:10:09
【问题描述】:

我正试图在 Scrapy 完成工作后存储一些数据(即在它完成了我问他的每个 URL 之后)。 每次 Scrapy 解析一些结果(通过蜘蛛类中的解析函数)时,我都会将一些信息附加到类本身的现有全局对象中。我想最后访问那个对象,如果可能的话,从 Python 脚本中做所有事情。这是我的蜘蛛代码:

from scrapy.spider import Spider
from scrapy.selector import Selector
from nltk.corpus import stopwords


from newsScrapy.items import NewsscrapyItem

class newsScrapySpider(Spider):
    name = "newsScrapy"
    start_urls = []

    global wordMatrix
    wordMatrix = {}

    global prefix
    prefix = "http://www.nytimes.com/indexes/"
    sufix = "/todayspaper/index.html"
    for year in range (2000,2015):
        for month in range (1,13):
            for day in range (1,32):
                if(month<10 and day<10):
                    start_urls.append (prefix+str(year)+"/"+"0"+str(month)+"/"+"0"+str(day))
                elif (month<10 and day>9):
                    start_urls.append (prefix+str(year)+"/"+"0"+str(month)+"/"+str(day))
                elif (month>9 and day<10):
                    start_urls.append (prefix+str(year)+"/"+str(month)+"/"+"0"+str(day))
                else:
                    start_urls.append (prefix+str(year)+"/"+str(month)+"/"+str(day))

    def parse(self, response):
        sel = Selector(response)
        items = []
        text = sel.xpath('//body//text()').re('(\w+)')

        item = NewsscrapyItem()

        item['body'] = text
        item['date'] = response.url.strip(prefix)

        items.append(item)

        for word in item['body']:
            word = word.strip(' ').strip(',').strip('\n')
            word = word.lower()
            if (not word in stopwords.words('english')):
                if(wordMatrix.__contains__((word, item['date']))):
                    wordMatrix[word,item['date']]+=1
                else:
                    wordMatrix[word, item['date']]=1


        # print wordMatrix
        return items

我们的想法是在抓取结束后访问 wordMatrix 变量(一旦收集了所有数据)并从另一个 Python 脚本中执行此操作(用于绘制每个示例)。 非常感谢!

【问题讨论】:

    标签: python web-crawler scrapy


    【解决方案1】:

    与您现有的导入一起:

    try:
        import cPickle as pickle
    except ImportError:
        import pickle
    

    然后就在return items之前:

    pickle.dump(wordMatrix, '/path/to/file/wordMatrix.data');
    

    在另一个脚本中,您可以使用以下方法加载此数据:

    try:
        import cPickle as pickle
    except ImportError:
        import pickle
    
    wordMatrix = pickle.load('/path/to/file/wordMatrix.data')
    

    Pickling 是一个序列化和反序列化任何 Python 对象的过程。 Python 标准库中有两个实现——pickle 是纯 Python,cPickle 是用 C 编写的,因此速度要快得多。不寻常的导入代码尝试导入更快的代码,但例如 IronPython 缺少 cPickle,在这种情况下后者被导入。两个模块的功能完全相同,并且共享相同的接口。

    【讨论】:

    • 感谢您的回答,但它不符合我的要求。使用该方法,每次调用时都会转储数据(而不仅仅是最后一次)。此外,我需要它是累积的数据,而不仅仅是最后一个请求。
    • 您没有提供调用parse(self, response) 的代码,但由于我的解决方案实际上存储了数据,您应该将其移动到您收集所有数据的地方。
    猜你喜欢
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2023-02-10
    • 1970-01-01
    相关资源
    最近更新 更多