Search code examples
pythonscrapyscrapy-splashsplash-js-render

Blank spaces between scraped values in Scrapy


I'm trying to use Scrapy to scrape some objects from the following page:

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

Using the following code:

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)

When i run the spider, the 'status' and 'business' variables returns like this:

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

But 'title' and 'city_date' returns like this:

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

I don't know why it returns those blank spaces between the scraped values, how can i scrape the results without the blank spaces or do i need to remove then after scraping?

(I'm also using splash to render the page, because it's a javascript-heavy page, but i don't think that this should affect the scraping)


Solution

  • The blank spaces usually comes because of <br> tags from HTML. This is very common in sites, unfortunately. What you can do to solve this, and this is why I use to, is join the list.

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

    Credtis to @Sven H. for the solution