How to handle infinite scroll pages in Ruby

Nowadays, most websites use different methods and techniques to decrease the load and data served to their clients’ devices. One of these techniques is the infinite scroll.

In this tutorial, we will see how we can scrape infinite scroll web pages using a  js_scenario , specifically the scroll_y and scroll_x features. And we will use  this page  as a demo. Only 9 boxes are loaded when we first open the page, but as soon as we scroll to the end of it, we will load 9 more, and that will keep happening each time we scroll to the bottom of the page.

First let’s make a request without the scroll_y parameter and see what the result looks like. We will use this code:


require 'net/http'
require 'net/https'
require 'addressable/uri'

# Classic (GET)
def no_scroll(user_url)
    uri = Addressable::URI.parse("https://app.scrapingbee.com/api/v1/")
    api_key = "YOUR-API-KEY"
    uri.query_values = {
      'api_key'  => api_key,
      'url' => user_url
    }
    uri = URI(uri)

    # Create client
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER

    # Create Request
    req =  Net::HTTP::Get.new(uri)

    # Fetch Request
    res = http.request(req)

    return res
rescue StandardError => e
    puts "HTTP Request failed (#{ e.message })"
end

content = no_scroll("https://demo.scrapingbee.com/infinite_scroll.html")
File.open("no_scrolling.html", 'w') { |file| file.write(content.body) }

And the result as you will see below the first 9 pre-loaded blocks.So for websites that have infinite scroll, you will not be able to extract information efficiently without scroll_y.

The code below will scroll to the end of the page and wait for 500 milliseconds two times, then save the result in an HTML document.


require 'net/http'
require 'net/https'
require 'addressable/uri'
require 'json'

# Classic (GET)
def scroll(user_url)
    uri = Addressable::URI.parse("https://app.scrapingbee.com/api/v1/")
    api_key = "YOUR-API-KEY"

    # Setting the js_scenario to scroll:
    js_scenario = {"instructions" => [
    {"scroll_y": 1080},
    {"wait": 500},
    {"scroll_y": 1080},
    {"wait": 500}
    ]}

    uri.query_values = {
      'api_key'  => api_key,
      'url' => user_url,
      'js_scenario' => js_scenario.to_json
    }
    uri = URI(uri)

    # Create client
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER

    # Create Request
    req =  Net::HTTP::Get.new(uri)

    # Fetch Request
    res = http.request(req)

    return res
rescue StandardError => e
    puts "HTTP Request failed (#{ e.message })"
end

content = scroll("https://demo.scrapingbee.com/infinite_scroll.html")
File.open("no_scrolling.html", 'w') { |file| file.write(content.body) }

And as you can see below, we managed to scrape 18 blocks. We can even go further and scrape more blocks if wanted by adding more scroll_y instructions.

 

Go back to tutorials