【问题标题】:Can't pickle local object 'run_spider.<locals>.f'无法腌制本地对象'run_spider.<locals>.f'
【发布时间】:2021-07-27 10:19:15
【问题描述】:

我一直在尝试在this question 上找到的 spyder 上运行以下代码:

import scrapy
import scrapy.crawler as crawler
from multiprocessing import Process, Queue
from twisted.internet import reactor

# your spider
class QuotesSpider(scrapy.Spider):
    name = "quotes"
    start_urls = ['http://quotes.toscrape.com/tag/humor/']

    def parse(self, response):
        for quote in response.css('div.quote'):
            print(quote.css('span.text::text').extract_first())


# the wrapper to make it run more times
def run_spider(spider):
    def f(q):
        try:
            runner = crawler.CrawlerRunner()
            deferred = runner.crawl(spider)
            deferred.addBoth(lambda _: reactor.stop())
            reactor.run()
            q.put(None)
        except Exception as e:
            q.put(e)

    q = Queue()
    p = Process(target=f, args=(q,))
    p.start()
    result = q.get()
    p.join()

    if result is not None:
        raise result
        
        
print('first run:')
run_spider(QuotesSpider)

print('\nsecond run:')
run_spider(QuotesSpider)

但是,当我运行它时,我收到以下错误:

AttributeError: Can't pickle local object 'run_spider.<locals>.f'

我已经看到建议的一个答案

Had small issue regarding 'AttributeError: Can't pickle local object 'run_spider.<locals>.f', but moving function called f outside resolved my issue, and I could run the code –

我尝试将函数 f 放在 run_spider 函数之外,甚至放在不同的文件中。但还是不行。

任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: python python-3.x scrapy multiprocessing web-crawler


    【解决方案1】:

    我尝试将函数 f 放在 run_spider 函数之外,甚至放在不同的文件中。但还是不行。

    你很亲密

    import scrapy
    import scrapy.crawler as crawler
    from multiprocessing import Process, Queue
    from twisted.internet import reactor
    
    
    # your spider
    class QuotesSpider(scrapy.Spider):
        name = "quotes"
        start_urls = ['http://quotes.toscrape.com/tag/humor/']
    
        def parse(self, response):
            for quote in response.css('div.quote'):
                print(quote.css('span.text::text').extract_first())
    
    
    def f(q, spider):
        try:
            runner = crawler.CrawlerRunner()
            deferred = runner.crawl(spider)
            deferred.addBoth(lambda _: reactor.stop())
            reactor.run()
            q.put(None)
        except Exception as e:
            q.put(e)
    
        return q
    
    
    # the wrapper to make it run more times
    def run_spider(spider):
        q = Queue()
        p = Process(target=f, args=(q, spider))
        p.start()
        result = q.get()
        p.join()
    
        if result is not None:
            raise result
    
    
    if __name__ == "__main__":
        print('first run:')
        run_spider(QuotesSpider)
    
        print('\nsecond run:')
        run_spider(QuotesSpider)
    

    输出:

    first run:
    “The person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.”
    “A day without sunshine is like, you know, night.”
    “Anyone who thinks sitting in church can make you a Christian must also think that sitting in a garage can make you a car.”
    “Beauty is in the eye of the beholder and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.”
    “All you need is love. But a little chocolate now and then doesn't hurt.”
    “Remember, we're madly in love, so it's all right to kiss me anytime you feel like it.”
    “Some people never go crazy. What truly horrible lives they must lead.”
    “The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.”
    “Think left and think right and think low and think high. Oh, the thinks you can think up if only you try!”
    “The reason I talk to myself is because I’m the only one whose answers I accept.”
    
    second run:
    “The person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.”
    “A day without sunshine is like, you know, night.”
    “Anyone who thinks sitting in church can make you a Christian must also think that sitting in a garage can make you a car.”
    “Beauty is in the eye of the beholder and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.”
    “All you need is love. But a little chocolate now and then doesn't hurt.”
    “Remember, we're madly in love, so it's all right to kiss me anytime you feel like it.”
    “Some people never go crazy. What truly horrible lives they must lead.”
    “The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.”
    “Think left and think right and think low and think high. Oh, the thinks you can think up if only you try!”
    “The reason I talk to myself is because I’m the only one whose answers I accept.”
    

    【讨论】:

    • 感谢您的回答。不幸的是,我收到此错误后:AttributeError: Can't get attribute 'f' on main' (built-in)>
    • @colla 你复制了准确​​的代码吗?,你在用 Jupyter 吗?
    • 不,我使用的是 Spyder,是的,我复制了确切的代码
    • @colla 我试图在 spyder 中运行它,虽然我没有收到错误,但它没有按预期工作。我不确定是什么问题,但它在不同的 IDE 上对我有用。
    猜你喜欢
    • 2021-12-22
    • 2022-11-16
    • 1970-01-01
    • 2020-09-22
    • 2022-01-02
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    相关资源
    最近更新 更多