Scrapy中刮取值之间的空格

2024-10-01 22:39:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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)

当我运行spider时,“status”和“business”变量返回如下:

['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']

但“标题”和“城市日期”返回如下:

[' ', ' ', '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重的页面,但我不认为这会影响抓取)


Tags: textselfcomresponsestatusitemngxpath
1条回答
网友
1楼 · 发布于 2024-10-01 22:39:28

空格通常来自HTML中的<br>标记。不幸的是,这在网站中非常常见。你能做什么来解决这个问题,这就是为什么我用,加入名单。你知道吗

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

向@Sven H.申请解决方案

相关问题 更多 >

    热门问题