複数のスパイダーを同時に実行している Scrapyd サーバーがあり、schedule.json エンドポイントを使用してスパイダーを 1 つずつ起動します。すべてのスパイダーは、パイプラインを使用して共通ファイルにコンテンツを書き込んでいます
class JsonWriterPipeline(object):
def __init__(self, json_filename):
# self.json_filepath = json_filepath
self.json_filename = json_filename
self.file = open(self.json_filename, 'wb')
@classmethod
def from_crawler(cls, crawler):
save_path='/tmp/'
json_filename=crawler.settings.get('json_filename', 'FM_raw_export.json')
completeName = os.path.join(save_path, json_filename)
return cls(
completeName
)
def process_item(self, item, spider):
line = json.dumps(dict(item)) + "\n"
self.file.write(line)
return item
スパイダーが実行されると、データが正しく収集され、アイテムがファイル XXXX.jl に保存され、スパイダーが正しく動作することがわかりますが、クロールされたコンテンツは共通ファイルに反映されません。スパイダーはうまく機能しているように見えますが、パイプラインはうまく機能しておらず、共通ファイルにデータを収集していません。
また、同時にファイルに書き込みを行っているのは 1 つのスパイダーだけであることに気付きました。