Json 用 python 解码
Json decoded with python
(Scrapy)我需要下一段代码的帮助:
def parse_item(self, response):
ml_item = MercadoItem()
#info de producto
ml_item['nombre'] = response.xpath('//h1[@class="title"]/text()').extract()
ml_item['web'] = response.xpath('/html/body/div[1]/div/div/div[1]/main/div/div[1]/div[2]/div[1]/div/div[4]/a/@href').extract()
script_data = response.xpath('string(/html/head/script[3]/text()').extract()
decoded_data = json.loads(script_data)
ml_item['datos'] = decoded_data["telephone"]
ml_item['direccion'] = response.xpath('/html/body/div[1]/div/div/div[1]/main/div/div[1]/div[2]/div[1]/div/span[2]/text()').extract()
self.item_count += 1
if self.item_count > 5:
raise CloseSpider('item_exceeded')
yield ml_item
我使用解码的 Json 仅获取 phone 号码,但控制台 return 我出错了
script_data 包含脚本
File "/mercadolibre-scrapy-master/mercado/spiders/spiderq.py", line 88
ml_item['direccion'] = response.xpath('/html/body/div[1]/div/div/div[1]/main/div/div[1]/div[2]/div[1]/div/span[2]/text()').extract()
^ IndentationError: unexpected indent
脚本是:
{"@context":"http://schema.org","@type":"LocalBusiness","name":"Clínica Dental Castellana 23","description":".TU CLÍNICA DENTAL DE REFERENCIA EN MADRID","telephone":"+34912298837","address":{"@type":"PostalAddress","streetAddress":"Castellana 23","addressLocality":"MADRID","addressRegion":"Madrid","postalCode":"28003"}}
检查错误报告的行,用于对齐该行的缩进与用于前一行的缩进不同,例如您可能前面有 4 个空格,后面有 1 个制表符,看起来可能是一样的,但它们对于 Python 解释器是不同的。
(Scrapy)我需要下一段代码的帮助:
def parse_item(self, response):
ml_item = MercadoItem()
#info de producto
ml_item['nombre'] = response.xpath('//h1[@class="title"]/text()').extract()
ml_item['web'] = response.xpath('/html/body/div[1]/div/div/div[1]/main/div/div[1]/div[2]/div[1]/div/div[4]/a/@href').extract()
script_data = response.xpath('string(/html/head/script[3]/text()').extract()
decoded_data = json.loads(script_data)
ml_item['datos'] = decoded_data["telephone"]
ml_item['direccion'] = response.xpath('/html/body/div[1]/div/div/div[1]/main/div/div[1]/div[2]/div[1]/div/span[2]/text()').extract()
self.item_count += 1
if self.item_count > 5:
raise CloseSpider('item_exceeded')
yield ml_item
我使用解码的 Json 仅获取 phone 号码,但控制台 return 我出错了 script_data 包含脚本
File "/mercadolibre-scrapy-master/mercado/spiders/spiderq.py", line 88 ml_item['direccion'] = response.xpath('/html/body/div[1]/div/div/div[1]/main/div/div[1]/div[2]/div[1]/div/span[2]/text()').extract()
^ IndentationError: unexpected indent
脚本是:
{"@context":"http://schema.org","@type":"LocalBusiness","name":"Clínica Dental Castellana 23","description":".TU CLÍNICA DENTAL DE REFERENCIA EN MADRID","telephone":"+34912298837","address":{"@type":"PostalAddress","streetAddress":"Castellana 23","addressLocality":"MADRID","addressRegion":"Madrid","postalCode":"28003"}}
检查错误报告的行,用于对齐该行的缩进与用于前一行的缩进不同,例如您可能前面有 4 个空格,后面有 1 个制表符,看起来可能是一样的,但它们对于 Python 解释器是不同的。