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:
<?php
// Get cURL resource
$ch = curl_init();
// Set base url & API key
$BASE_URL = "https://app.scrapingbee.com/api/v1/?";
$API_KEY = "YOUR-API-KEY";
// Set parameters
$parameters = array(
'api_key' => $API_KEY,
'url' => 'https://demo.scrapingbee.com/infinite_scroll.html' // The URL to scrape
);
// Building the URL query
$query = http_build_query($parameters);
// Set the URL for cURL
curl_setopt($ch, CURLOPT_URL, $BASE_URL.$query);
// Set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
// Return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Send the request and save response to $response
$response = curl_exec($ch);
// Stop if fails
if (!$response) {
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
$file = fopen("no_scrolling.html", "w");
fwrite($file, $response);
fclose($file);
// Close curl resource to free up system resources
curl_close($ch);
?>
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.
<?php
// Get cURL resource
$ch = curl_init();
// Set base url & API key
$BASE_URL = "https://app.scrapingbee.com/api/v1/?";
$API_KEY = "YOUR-API-KEY";
$js_scenario = ["instructions" => [ // Setting the JS_Scenario variable with the instructions:
["scroll_y" => 1080], // Scroll by y axis (Vertically)
["wait" => 500], // Wait for 500 milliseconds
["scroll_y" => 1080],
["wait" => 500]
]];
$js_scenario = json_encode($js_scenario);
// Set parameters
$parameters = array(
'api_key' => $API_KEY,
'url' => 'https://demo.scrapingbee.com/infinite_scroll.html', // The URL to scrape
'js_scenario' => $js_scenario
);
// Building the URL query
$query = http_build_query($parameters);
// Set the URL for cURL
curl_setopt($ch, CURLOPT_URL, $BASE_URL.$query);
// Set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
// Return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Send the request and save response to $response
$response = curl_exec($ch);
// Stop if fails
if (!$response) {
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
$file = fopen("no_scrolling.html", "w");
fwrite($file, $response);
fclose($file);
// Close curl resource to free up system resources
curl_close($ch);
?>
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