【发布时间】:2020-06-16 05:04:29
【问题描述】:
我正在尝试使用蜘蛛名称和日期命名管道输出文件。 我编写了一小段代码来调用文件的日期。 问题在于蜘蛛名称。 下面介绍两种方法。两者都有效,但我想了解其中的细微差别。 第一种方法从 PyCharm 生成一个我不理解的建议,特别是因为它遵循 scrapy docs 的示例。
第一种方法:
# pipelines.py
# Cannot add spider as input to class Pipeline
class CsvPipeline(object):
def open_spider(self, spider):
# Call to put file in correct directory
redefine_dir(spider, file_type='csv')
# Call to name file correctly
name = dated_filename(spider, '.csv')
# PyCharm insists the following two lines should be in __init__; Why?
# Only seem to be able to have spider as input to open_spider and not the class
self.file = open(name, 'wb')
self.exporter = CsvItemExporter(self.file)
self.exporter.start_exporting()
def close_spider(self):
self.exporter.finish_exporting()
self.file.close()
def process_item(self, item):
self.exporter.export_item(item)
return item
第二种方法:
class JsonPipeline(object):
def __init__(self):
# As I cannot figure out how spider is an input to __init__ I have to create a temporary file
# This temporary file is renamed later.
print('Current working directory:', os.getcwd())
if os.getcwd() == 'C:\\PycharmProjects\\ABC\\abc\\run':
os.chdir('..')
elif os.getcwd() == 'C:\\PycharmProjects\\ABC':
os.chdir('abc')
print('Current working directory now:', os.getcwd())
# Temporary file created
self.file = open('data/raw.json', 'wb')
self.exporter = JsonItemExporter(self.file)
self.exporter.start_exporting()
def close_spider(self, spider):
self.exporter.finish_exporting()
self.file.close()
new_name = dated_filename(spider, '.json')
print('Current working directory:', os.getcwd())
if os.getcwd() == 'C:\\PycharmProjects\\ABC\\abc\\run':
os.chdir('..')
elif os.getcwd() == 'C:\\PycharmProjects\\ABC':
os.chdir('abc')
print('Current working directory now:', os.getcwd())
# Rename file to dated filename
rename('data/raw.json', new_name)
def process_item(self, item):
self.exporter.export_item(item)
return item
- 是否可以包含蜘蛛作为 Pipeline 类的输入?如果有,怎么做?
- 是否可以包含蜘蛛作为管道类 init 的输入?如果有,怎么做?
- 为什么 PyCharm 坚持我应该将 self.file 和 self.exporter 放在 init 下?
- 有更好的想法吗?
【问题讨论】:
-
第一种方法有什么问题,除了 PyCharm 警告?您可以通过使用
None将这些变量定义为__init__中的值来使它们静音。 -
我发现这些方法中的一种或两种会导致关闭蜘蛛的问题。需要进一步挖掘以了解原因。我理解为什么 PyCharm 想要在 init 中使用这些:最初而不是稍后配置类参数。如果我使用
None文件参数未定义为_io.FileIO并创建一个异常错误,因为它不能被爬虫/管道写入。