【问题标题】:Running scrapy spider from script results in error ImportError: No module named scrapy从脚本运行 scrapy spider 会导致错误 ImportError: No module named scrapy
【发布时间】:2019-12-10 11:02:12
【问题描述】:

我已经安装了 scrapy 并创建了一个可以在命令行运行时使用命令 scrapy crawl getBUCPower 的蜘蛛。我的问题是,当发生特定事情时,我需要从另一个脚本运行蜘蛛。我有一个单独的 python 脚本,现在只是一个测试,但它尝试按行运行蜘蛛:

execfile("../scrapy/data_spider/data_spider/spiders/getBUCPower.py")

当我运行该脚本时,我立即收到错误:

文件“getBUCPower.py”,第 2 行,在
导入scrapy
ImportError: 没有名为 scrapy 的模块

我已经正确安装了scrapy,因为它在我运行scrapy crawl 命令时可以正常工作,所以我现在不知道问题是什么。

这是我的蜘蛛

import scrapy
from scrapy.crawler import CrawlerProcess
from scrapy import Spider
from scrapy.loader import ItemLoader

class DataSpider(Spider):
#sets the name of the spider
name = 'getBUCPower'
#sets the pipeline the spider should use
custom_settings = {
    'ITEM_PIPELINES': {
        'data_spider.pipelines.CsvPipeline': 300
    }
}
#sets the url the spider should crawl
allowed_domains = [ip]
start_urls = [ipHTTP]


def parse(self, response):
    #sets the information the sipder should grab
    BUCPower = response.xpath('//*[@id="mmwpaTxPower"]/text()').extract_first()
    #returns the information
    yield{"BUCPower" : BUCPower}

process = CrawlerProcess(settings={
'FEED_FORMAT': 'json',
'FEED_URI': 'items.json'
})

process.crawl(DataSpider)
process.start()

我目前有 scrapy 1.6 并且正在使用 Python 2.7.16

预期的结果是从另一个脚本运行蜘蛛,但导入 scrapy 时出现问题。

【问题讨论】:

  • 如何运行包含execfile() 的python 脚本?使用的是什么版本的 Python?
  • 目前我只是通过“python callGetBucPower.py”从命令行运行它,我使用的是 2.7.16
  • 你从python --version得到什么输出?你的PYTHONPATH 是什么?
  • Python 2.7.16 :: Anaconda, Inc. 我不确定如何找到我的 PYTHONPATH
  • 您在使用 Anaconda?你是如何安装scrapy的?

标签: python web-scraping import scrapy


【解决方案1】:

以管理员身份运行 anaconda 提示符,然后简单地通过命令安装 scrapy

pip install scrapy

【讨论】:

    【解决方案2】:

    在 anaconda/miniconda 提示符下编写以下代码:

    conda install scrapy
    

    【讨论】:

      【解决方案3】:

      安装 protego 包为我解决了这个问题。

      conda install protego 要么 pip install protego

      【讨论】:

        猜你喜欢
        • 2020-09-26
        • 1970-01-01
        • 1970-01-01
        • 2017-01-24
        • 2015-10-13
        • 1970-01-01
        • 2014-03-18
        • 1970-01-01
        • 2018-10-04
        相关资源
        最近更新 更多