【问题标题】:Json decoded with python [duplicate]用python解码的Json [重复]
【发布时间】:2019-10-27 18:49:25
【问题描述】:

(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 来获取电话号码,但控制台返回错误 script_data 包含脚本

文件“/mercadolibre-scrapy-master/mercado/spiders/spiderq.py”,第 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"}}

【问题讨论】:

  • 您在缩进中混合了制表符和空格。

标签: python json scrapy web-crawler


【解决方案1】:

检查错误报告的行,用于对齐该行的缩进与用于上一行的缩进不同,例如您可能有前 4 个空格和后 1 个制表符,可能看起来相同,但它们Python 解释器是不同的。

【讨论】:

  • 我修复了缩进,但现在我遇到了导入问题。 “无法导入 Scrapy”
猜你喜欢
  • 2011-01-20
  • 2017-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-16
  • 1970-01-01
  • 1970-01-01
  • 2013-04-07
相关资源
最近更新 更多