少し複雑ですが、それでも機能します。
JSON 出力で xpath を使用することに興味がある場合は..
免責事項 : 最適なソリューションではない場合があります。誰かがこのアプローチを改善した場合は+1。
dicttoxml パッケージをインストールします (pip を推奨)
-scrapy の従来の Request モジュールを使用して出力をダウンロードします
クモで:
from scrapy.selector import XmlXPathSelector
import lxml.etree as etree
request = Request(link, callback=self.parse_resp)
yield request
def parse_resp(self,response):
json=response.body
#Now load the contents using python's JSON module
json_dict = json.loads(json)
#transform the contents into xml using dicttoxml
xml = dicttoxml.dicttoxml(json_dict)
xml = etree.fromstring(xml)
#Apply scrapy's XmlXPathSelector module,and start using xpaths
xml = XmlXPathSelector(text=xml)
data = xml.select(".//*[@id='count']/text()").extract()
return data
これを行ったのは、すべてのスパイダーのすべての xpath を 1 か所 (config-files) に維持しているためです。