Scrapy - Extracting page links generated with JS function

Viewed 1012

I'm trying to crawl this page and the links for the next pages. https://www.chavesnamao.com.br/carros/usados/brasil/

<a href="javascript:;" onclick="goToPage(2); return false;" rel="next" title="Página 2">2</a>

The links that are leading to next pages are generated with this goToPage() function. The function generates a query string like this;

{"pg":"1","or":"","c":"","e":"","r":"50","view":"tabela"}

pg is the page parameter of the request. Is there any systematic way to implement a Rule which follows those pages and call a parse function or do I need to hardcode a loop which increments the pg parameter and sends request every time? This is my question and my spider is below.

class MySpider(CrawlSpider):

    name = 'myspider'

    start_urls = [
        'https://www.chavesnamao.com.br/carros/usados/brasil/#{%22pg%22:%221%22,%22or%22:%22%22,%22c%22:%22%22,%22e%22:%22%22,%22r%22:%2250%22,%22view%22:%22tabela%22}',
    ]

    def parse(self, response):

        #logging.warning('parse function called on %s', response.url)

        #from scrapy.shell import inspect_response
        #inspect_response(response, self)

        items = response.css('div.view ul#listagem li div a.description::attr(href)').getall()

        for item in items:
            yield scrapy.Request(
                url='{}'.format(item),
                method='GET',
                callback=self.parse_items,
                headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
            )

    def parse_items(self, response):

        # Crawling the item without any problem here
0 Answers
Related