Scrapy 中抓取的值之间的空格



我正在尝试使用Scrapy从以下页面抓取一些对象:

https://www.reclameaqui.com.br/indices/lista_reclamacoes/?id=9980&page=1&size=10&status=ALL

使用以下代码:

class MySpider(scrapy.Spider):
name = 'reclame_aqui'
allowed_domains = ["https://www.reclameaqui.com.br"]
start_urls = ["https://www.reclameaqui.com.br/indices/lista_reclamacoes/?id=9980&page=1&size=10&status=ALL"]
def start_requests(self):
for url in self.start_urls:
yield SplashRequest(url, self.parse,
endpoint='render.html',
args={'wait': 0.5},
)
def parse(self, response):
title = response.xpath('//*[contains(@class, "complaint-item ng-scope")]//*[contains(@class, "complain-status-title")]//text()').extract()
status = response.xpath('//*[contains(@class, "complaint-item ng-scope")]//*[contains(@class, "status-text ng-binding")]//text()').extract()
business = response.xpath('//*[contains(@class, "complaint-item ng-scope")]//*[contains(@class, "business-name ng-binding")]//text()').extract()
city_date = response.xpath('//*[contains(@class, "complaint-item ng-scope")]//*[contains(@class, "detail-city-date ng-binding")]//text()').extract()
print(title)
print(status)
print(business)
print(city_date)

当我运行爬虫时,"状态"和"业务"变量返回如下:

['Respondida', 'Resolvido', 'Resolvido', 'Resolvido', 'Não Respondida', 'Resolvido', 'Resolvido', 'Resolvido', 'Resolvido', 'Resolvido']
['Disk Ingressos', 'Disk Ingressos', 'Disk Ingressos', 'Disk Ingressos', 'Disk Ingressos', 'Disk Ingressos', 'Disk Ingressos', 'Disk Ingressos', 'Disk Ingressos', 'Disk Ingressos']

但是"标题"和"city_date"返回如下:

[' ', ' ', 'Isso é [Editado pelo Reclame Aqui]', ' ', ' ', ' ', ' ', 'prometeram e não cumpriram', ' ', ' ', ' ', ' ', 'Telemarketing Ineficiênte e chato', ' ', ' ', ' ', ' ', 'Cobranças indevida e não resolvem!', ' ', ' ', ' ', ' ', 'Agendamento de Instalação', ' ', ' ', ' ', ' ', 'Falta de respeito com o cliente.', ' ', ' ', ' ', ' ', 'Não conseguem colocar meu telefone fixo para funcionar', ' ', ' ', ' ', ' ', 'Telefone sem funcionamento ', ' ', ' ', ' ', ' ', 'Cobrança hero', ' ', ' ', ' ', ' ', 'Agendamento de retirada de Modem para devolução', ' ', ' ']
[' ', 'Curitiba', ' ', ' 25/09/18 às 19h33 ', ' ', ' ', 'Curitiba', ' ', ' 25/09/18 às 17h13 ', ' ', ' ', 'Itabuna', ' ', ' 20/09/18 às 13h18 ', ' ', ' ', 'Curitiba', ' ', ' 19/09/18 às 09h37 ', ' ', ' ', 'Araucária', ' ', ' 17/09/18 às 21h18 ', ' ', ' ', 'Curitiba', ' ', ' 14/09/18 às 21h04 ', ' ', ' ', 'São José dos Pinhais', ' ', ' 12/09/18 às 16h56 ', ' ', ' ', 'Curitiba', ' ', ' 12/09/18 às 05h45 ', ' ', ' ', 'Londrina', ' ', ' 11/09/18 às 15h53 ', ' ', ' ', 'Curitiba', ' ', ' 10/09/18 às 11h49 ', ' ']

我不知道为什么它会在抓取值之间返回这些空格,如何在没有空格的情况下抓取结果,还是需要在抓取后删除?

(我也使用splash来渲染页面,因为它是一个javascript繁重的页面,但我认为这不会影响抓取(

空格通常是因为来自 HTML 的<br>标签。不幸的是,这在网站上很常见。你可以做些什么来解决这个问题,这就是为什么我习惯这样做,就是加入列表。

[x for x in city_date if x.strip() != ""]

克雷蒂斯到@Sven H.的解决方案

最新更新