Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Python脚本内部运行scrapy-CSV exporter不会';行不通_Python_Python 2.7_Export_Twisted_Scrapy - Fatal编程技术网

从Python脚本内部运行scrapy-CSV exporter不会';行不通

从Python脚本内部运行scrapy-CSV exporter不会';行不通,python,python-2.7,export,twisted,scrapy,Python,Python 2.7,Export,Twisted,Scrapy,当我从命令行运行它时,我的scraper工作得很好,但是当我尝试在python脚本中运行它(使用Twisted概述的方法)时,它不会像通常那样输出两个CSV文件。我有一个创建和填充这些文件的管道,其中一个使用CsvItemExporter(),另一个使用writeCsvFile()。代码如下: class CsvExportPipeline(object): def __init__(self): self.files = {} @classmethod

当我从命令行运行它时,我的scraper工作得很好,但是当我尝试在python脚本中运行它(使用Twisted概述的方法)时,它不会像通常那样输出两个CSV文件。我有一个创建和填充这些文件的管道,其中一个使用CsvItemExporter(),另一个使用writeCsvFile()。代码如下:

class CsvExportPipeline(object):

    def __init__(self):
        self.files = {}

    @classmethod
    def from_crawler(cls, crawler):
        pipeline = cls()
        crawler.signals.connect(pipeline.spider_opened, signals.spider_opened)
        crawler.signals.connect(pipeline.spider_closed, signals.spider_closed)
        return pipeline

    def spider_opened(self, spider):
        nodes = open('%s_nodes.csv' % spider.name, 'w+b')
        self.files[spider] = nodes
        self.exporter1 = CsvItemExporter(nodes, fields_to_export=['url','name','screenshot'])
        self.exporter1.start_exporting()

        self.edges = []
        self.edges.append(['Source','Target','Type','ID','Label','Weight'])
        self.num = 1

    def spider_closed(self, spider):
        self.exporter1.finish_exporting()
        file = self.files.pop(spider)
        file.close()

        writeCsvFile(getcwd()+r'\edges.csv', self.edges)

    def process_item(self, item, spider):
        self.exporter1.export_item(item)

        for url in item['links']:
            self.edges.append([item['url'],url,'Directed',self.num,'',1])
            self.num += 1
        return item
以下是我的文件结构:

SiteCrawler/      # the CSVs are normally created in this folder
    runspider.py  # this is the script that runs the scraper
    scrapy.cfg
    SiteCrawler/
        __init__.py
        items.py
        pipelines.py
        screenshooter.py
        settings.py
        spiders/
            __init__.py
            myfuncs.py
            sitecrawler_spider.py
刮板似乎在所有其他方面都正常工作。命令行末尾的输出表明已爬网预期的页面数,并且爬行器似乎已正常完成。我没有收到任何错误消息

----编辑:----

在管道中插入打印语句和语法错误无效,因此管道似乎被忽略。为什么会这样

以下是运行scraper(runspider.py)的脚本的代码:


在我的项目中,我使用os.system调用另一个python脚本中的零碎代码

import os
os.chdir('/home/admin/source/scrapy_test')
command = "scrapy crawl test_spider -s FEED_URI='file:///home/admin/scrapy/data.csv' -s LOG_FILE='/home/admin/scrapy/scrapy_test.log'"
return_code = os.system(command)
print 'done'
将“from scrapy.settings import settings”替换为“from scrapy.utils.project import get\u project\u settings as settings”,修复了该问题

找到了解决办法。没有提供解决方案的解释

提供了如何从Python脚本内部运行Scrapy的示例

编辑:


在更详细地阅读了alecxe的文章之后,我现在可以看到“from scrapy.settings import settings”和“from scrapy.utils.project import get_project_settings as settings”之间的区别。后者允许您使用项目的设置文件,而不是解除设置文件。阅读alecxe的帖子(链接到上面)了解更多详细信息。

这些文件可以写在其他地方吗?您可以检查输出文件路径还是使用绝对文件路径?我想这与实际使用的设置有关。日志开头说了什么?您应该已经启用了我正在查看和的所有中间件和管道。也许您必须使用
爬网设置(settings.module.to.use)
。至少您应该能够通过分离
mysettings=CrawlerSettings(settings.modules.to.use)
来签入runspider.py,也许可以使用
mysettings.get(setting_name)
打印出这些设置中的一些值,然后
crawler=crawler(mysettings)…。
可能就是这样:,看到了吗!我将来可能也需要这个。你可以发布自己的答案,并说明你是如何解决的。
import os
os.chdir('/home/admin/source/scrapy_test')
command = "scrapy crawl test_spider -s FEED_URI='file:///home/admin/scrapy/data.csv' -s LOG_FILE='/home/admin/scrapy/scrapy_test.log'"
return_code = os.system(command)
print 'done'