【问题标题】:Scrapy throws an error when run using crawlerprocessScrapy 使用 crawlerprocess 运行时抛出错误
【发布时间】:2019-04-01 16:39:02
【问题描述】:

我用 python 编写了一个脚本,使用 scrapy 从网站收集不同帖子的名称及其链接。当我从命令行执行我的脚本时,它可以完美运行。现在,我的意图是使用CrawlerProcess() 运行脚本。我在不同的地方寻找类似的问题,但我找不到任何直接的解决方案或任何更接近的解决方案。但是,当我尝试按原样运行它时,出现以下错误:

从 stackoverflow.items 导入 StackoverflowItem ModuleNotFoundError:没有名为“stackoverflow”的模块

这是我目前的脚本 (stackoverflowspider.py):

from scrapy.crawler import CrawlerProcess
from stackoverflow.items import StackoverflowItem
from scrapy import Selector
import scrapy

class stackoverflowspider(scrapy.Spider):
    name = 'stackoverflow'
    start_urls = ['https://stackoverflow.com/questions/tagged/web-scraping']

    def parse(self,response):
        sel = Selector(response)
        items = []
        for link in sel.xpath("//*[@class='question-hyperlink']"):
            item = StackoverflowItem()
            item['name'] = link.xpath('.//text()').extract_first()
            item['url'] = link.xpath('.//@href').extract_first()
            items.append(item)
        return items

if __name__ == "__main__":
    c = CrawlerProcess({
        'USER_AGENT': 'Mozilla/5.0',   
    })
    c.crawl(stackoverflowspider)
    c.start()

items.py 包括:

import scrapy

class StackoverflowItem(scrapy.Item):
    name = scrapy.Field()
    url = scrapy.Field()

这是树: Click to see the hierarchy

我知道我可以通过这种方式取得成功,但我只想用我上面尝试的方式完成任务:

def parse(self,response):
    for link in sel.xpath("//*[@class='question-hyperlink']"):
        name = link.xpath('.//text()').extract_first()
        url = link.xpath('.//@href').extract_first()
        yield {"Name":name,"Link":url}

【问题讨论】:

    标签: python python-3.x web-scraping scrapy scrapy-spider


    【解决方案1】:

    尽管@Dan-Dev 向我展示了正确方向的方法,但我还是决定提供一个完美无瑕的完整解决方案。

    除了我在下面粘贴的内容之外,什么都没有改变:

    import sys
    #The following line (which leads to the folder containing "scrapy.cfg") fixed the problem
    sys.path.append(r'C:\Users\WCS\Desktop\stackoverflow')
    from scrapy.crawler import CrawlerProcess
    from stackoverflow.items import StackoverflowItem
    from scrapy import Selector
    import scrapy
    
    
    class stackoverflowspider(scrapy.Spider):
        name = 'stackoverflow'
        start_urls = ['https://stackoverflow.com/questions/tagged/web-scraping']
    
        def parse(self,response):
            sel = Selector(response)
            items = []
            for link in sel.xpath("//*[@class='question-hyperlink']"):
                item = StackoverflowItem()
                item['name'] = link.xpath('.//text()').extract_first()
                item['url'] = link.xpath('.//@href').extract_first()
                items.append(item)
            return items
    
    if __name__ == "__main__":
        c = CrawlerProcess({
            'USER_AGENT': 'Mozilla/5.0',   
        })
        c.crawl(stackoverflowspider)
        c.start()
    

    再一次,在脚本中包含以下内容解决了问题

    import sys
    #The following line (which leads to the folder containing "scrapy.cfg") fixed the problem
    sys.path.append(r'C:\Users\WCS\Desktop\stackoverflow')
    

    【讨论】:

      【解决方案2】:

      这是一个python路径问题。 最简单的方法是调用它显式设置 python 路径,即从包含 scrapy.cfg(更重要的是 stackoverflow 模块)的目录运行:

      PYTHONPATH=. python3 stackoverflow/spiders/stackoverflowspider.py
      

      这会将 python 路径设置为包含当前目录 (.)。

      有关替代方案,请参阅https://www.daveoncode.com/2017/03/07/how-to-solve-python-modulenotfound-no-module-named-import-error/

      【讨论】:

        猜你喜欢
        • 2022-01-25
        • 2017-07-12
        • 2020-10-29
        • 2016-06-24
        • 2018-08-01
        • 2021-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多