Esempio n. 1
0
	def parse_page(self, response):
		# extract the json response from the tag: <script id="__NEXT_DATA__" type="application/json”>
		jsonresponse = json.loads(response.css('#__NEXT_DATA__::text').extract()[0])
		# access the historical data within the JSON object 
		nestedJson = jsonresponse['props']['initialState']['cryptocurrency']['ohlcvHistorical']
		# retrieve the id of the crypto (a key value)
		id = [str(k) for k in nestedJson.keys()][0]
		# get the name of the respective crypto 
		name = nestedJson[id]['name']
		# save the ticker symbol 
		ticker = jsonresponse['props']['initialState']['cryptocurrency']['ohlcvHistorical'][id]['symbol']
		# accesss the historical data: e.g. Open, Close, High, Low, etc. 
		data = jsonresponse['props']['initialState']['cryptocurrency']['ohlcvHistorical'][id]['quotes']
		for d in data: 
			loader = ItemLoader(item=CryptoItem())
			loader.default_input_processor = MapCompose(str)
			loader.default_ouput_processor = Join('')

			for (field, path) in self.jmes_paths.items():
				loader.add_value(field, SelectJmes(path)(d))
			loader.add_value("Name", name)
			loader.add_value("Ticker", ticker)

			yield loader.load_item()