在保存資料的時候,兩個函式回傳的兩個item,而且所有設定都是正常的,items.py里面也是寫的兩個class,爬蟲檔案也匯入了這兩個class,但是程式跑起來以后,兩個json檔案的資料一毛一樣,有個item根本就沒有存下來,改怎么設定啊。
item.py中的兩個class(后面定義的具體內容省略了的):
class FangtxItem(scrapy.Item):
class FangtxesfItem(scrapy.Item):
爬蟲檔案匯入了兩個class:
from fangtx.items import FangtxItem,FangtxesfItem
回傳的兩個item也沒有問題(兩個item在不同的函式中):
item = FangtxItem(name = name,price = price,province = province,city = city)
item = FangtxesfItem(room = room,area = area,detail_url = detail_url,info = info ,place = place,province = province,city = city)
pipeline.py中的代碼:
class FangtxPipeline(object):
def __init__(self):
self.newhouse_fp = open('newhouse.json','wb')
self.esf_fp = open('esf.json','wb')
self.esf_exporter = JsonLinesItemExporter(self.esf_fp,ensure_ascii = False)
self.newhouse_exporter = JsonLinesItemExporter(self.newhouse_fp,ensure_ascii = False)
self.newhouse_exporter.start_exporting()
self.esf_exporter.start_exporting()
def process_item(self, item,spider):
self.newhouse_exporter.export_item(item)
self.esf_exporter.export_item(item)
return item
def close_spider(self,spider):
self.newhouse_exporter.finish_exporting()
self.esf_exporter.finish_exporting()
setting已經開啟:
ITEM_PIPELINES = {
'fangtx.pipelines.FangtxPipeline': 300,
}
然后兩個json檔案內容一毛一樣,,我吐了。。。
uj5u.com熱心網友回復:
取不同的名字uj5u.com熱心網友回復:
def process_item(self, item,spider):if spider.name =="":
pass
elif spider.name =="":
pass
uj5u.com熱心網友回復:
只有一個spider
uj5u.com熱心網友回復:
只有一個spider
uj5u.com熱心網友回復:
那就在item里面做判斷,給item一個標志,比如 item["flag"] = "first"
def process_item(self, item,spider):
if item.get("flag") =="first":
pass
else:
pass
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/28349.html
上一篇:如何根據電腦時鐘,在每個小時的5分鐘。10分鐘。15分鐘,。。。。45分鐘,50分鐘,55,分鐘,00分鐘開始運行程式,
下一篇:Python怎么設定佇列
