【发布时间】: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