只是爲了說明:我不是一個有經驗的程序員,不要生我的氣...... 我正在探索scrapy的可能性(我有一些Python編程技巧)。scrapy多個項目類與他們內的提取方法
刮痧網站:讓我們來想象我們能有從opengraph提取一些信息(OG :)像「標題」,「鏈接」和「說明」,以及其它信息從schema.org,像'作者',最後我們想要'標題','網址','描述'和'日期',可以從HTML提取「正常」XPath只是如果沒有可用從opengraph og :)和schema.org。
我創建了3個項目類OpengraphItem(Item),SchemaItem(Item)和MyItem(Item),在分離的.py文件中。 每個類內將有一個提取功能提取的字段,如下例所示:
class OpengraphItem(Item):
title = Field()
url = Field()
description = Field()
def extract(self, hxs):
self.title = hxs.xpath('/html/head/meta[@property="og:title"]/@content').extract()
self.url = hxs.xpath('/html/head/meta[@property="og:url"]/@content').extract()
self.description = hxs.xpath('/html/head/meta[@property="og:description"]/@content').extract()
然後在蜘蛛代碼,所述提取物的功能將被調用這樣的:
def parse_item(self, response):
hxs = HtmlXPathSelector(response)
my_item = MyItem()
item_opengraph = OpengraphItem()
item_opengraph.extract(hxs)
item_schema = SchemaItem()
item_schema.extract(hxs)
my_item['date']= hxs.xpath('/html/body//*/div[@class="reviewDate"]/span/time[@class="dtreviewed"]/@content').extract()
my_item['title'] = item_opengraph.get('title', None)
my_item['url'] = item_opengraph.get('url', None)
my_item['description'] = item_opengraph.get('description', None)
if my_item['url'] == None:
my_item['url'] = response.url
if my_item['title'] == None:
my_item['title'] = hxs.xpath('/html/head/title/text()').extract()
if my_item['description'] == None:
my_item['description'] = hxs.xpath('/html/head/meta[@name="description"]/@content').extract()
return my_item
這有意義嗎? 在物品類中創建提取方法是很方便的嗎?
我看了一下其他的問題: scrapy crawler to pass multiple item classes to pipeline - 我不知道是否只有一個items.py與多個不同的類內部是正確的。
Scrapy item extraction scope issue和scrapy single spider to pass multiple item classes to pipeline - 我應該有一個Itempipeline嗎?我不熟悉這些,但在scrapy文檔中說明了它的用途,我認爲它不適合這個問題。和項目裝載機?
我忽略了部分代碼。
是的。你可以把類放在不同的文件中,或放在同一個文件中。如果他們在不同的文件中,請確保您正確導入它們。 – MattDMo
看來你在問你很多問題。如果你提出了不同的問題,每個具體的疑問或問題都可能會更清楚。 –