Hatjhie
Hatjhie

Reputation: 1365

Python Web Scraping in Pagination in Single Page Application

I am currently researching on how to scrape web content using python in pagination driven by javascript in single page application (SPA).

For example, https://angular-8-pagination-example.stackblitz.io/

I googled and found that using Scrapy is not possible to scrape javascript / SPA driven content. It needs to use Splash. I am new to both Scrapy and Splash. Is this correct?

Also, how do I call the javascript pagination method? I inspect the element, it's just an anchor without href and javascript event.

Please advise.

Thank you,

Hatjhie

Upvotes: 1

Views: 2157

Answers (1)

TheGr8Destructo
TheGr8Destructo

Reputation: 96

You need to use a SpalshRequest to render the JS. You then need to get the pagination text. Generally I use re.search with the appropriate regex pattern to extract the relevent numbers. You can then assign them to current page variable and total pages variables.

Typically a website will move to the next page by incrementing ?page=x or ?p=x at the end of the url. You can then increment this value to scrape all the relevant pages.

The overall pattern looks like this:

import scrapy
from scrapy_splash import SplashRequest
import re

from ..items import Item

proxy ='http//your.proxy.com:PORT'

current_page_xpath='//div[your x path selector]/text()'
last_page_xpath='//div[your other x path selector]/text()'

class spider(scrapy.Spider):

    name = 'my_spider'
    allowed_domains =['domain.com']

    start_urls =['https://www.domaintoscrape.com/page=1']
                 
    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(url=url, callback=self.parse, meta ={'proxy':proxy})

     def get_page_nbr(value):
  
      #you may need more complex regex to get page numbers.
      #most of the time they are in form "page X of Y"
      #google is your friend

      if re.search('\d+',value):
           value = re.search('\d+',value)
           value = value[0]
      else:
           value =None
      return  value

    def parse(self, response):
            #get last and current page from response:

            last_page = page_response.xpath(last_page_xpath).get()
            current_page = page_response.xpath(current_page_xpath).get()

            #do something with your response 
            # if current page is less than last page make another request by incrmenenting the page in the URL

            if current_page < last_page:
                ajax_url = response.url.replace(f'page={int(current_page)}',f'page={int(current_page)+1}')
                yield scrapy.Request(url=ajax_url, callback=self.parse, meta ={'proxy':proxy})

            #optional
            if current_page == last_page:
                print(f'processed {last_page} items for {response.url}')

finally, its worth having a look on Youtube as there are a number of tutorials on scrapy_splash and pagination.

Upvotes: 1

Related Questions