下面是Python脚本,我使用调用scrapy的答案
从剧本总是块脚本执行经过刮Scrapy抓取
def stop_reactor():
reactor.stop()
dispatcher.connect(stop_reactor, signal=signals.spider_closed)
spider = MySpider(start_url='abc')
crawler = Crawler(Settings())
crawler.configure()
crawler.crawl(spider)
crawler.start()
log.start()
log.msg('Running reactor...')
reactor.run() # the script will block here until the spider is closed
log.msg('Reactor stopped.')
这里是我的pipelines.py代码
from scrapy import log,signals
from scrapy.contrib.exporter import JsonItemExporter
from scrapy.xlib.pydispatch import dispatcher
class scrapermar11Pipeline(object):
def __init__(self):
self.files = {}
dispatcher.connect(self.spider_opened , signals.spider_opened)
dispatcher.connect(self.spider_closed , signals.spider_closed)
def spider_opened(self,spider):
file = open('links_pipelines.json' ,'wb')
self.files[spider] = file
self.exporter = JsonItemExporter(file)
self.exporter.start_exporting()
def spider_closed(self,spider):
self.exporter.finish_exporting()
file = self.files.pop(spider)
file.close()
def process_item(self, item, spider):
self.exporter.export_item(item)
log.msg('It reached here')
return item
此代码是从这里取
Scrapy ::使用JSON出口问题
当我运行像这样的履带式
scrapy crawl MySpider -a start_url='abc'
与预期输出链接文件被创建。但是当我执行python脚本虽然履带运行作为倾倒scrapy统计类似于以前的运行它不会创建任何文件。 我认为,正如在第一种方法得到创建的文件。如何做我拿到剧本到输出的文件有一个错误在python脚本?