【发布时间】: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