If you are crawling a page that requires you to render the Javascript on the page to scrape the data you need, then we can fetch these pages using a headless browser.
To render Javascript, simply set render=true
and we will use a headless Google Chrome instance to fetch the page. This feature is available on all plans.
Pass the JavaScript rendering parameter within the URL :
Copy <? php $url =
"http://api.scraperapi.com?api_key=APIKEY&url=http://httpbin.org/ip&render=true" ; $ch = curl_init () ; curl_setopt ( $ch , CURLOPT_URL , $url ) ; curl_setopt ( $ch , CURLOPT_RETURNTRANSFER ,
TRUE ) ; curl_setopt ( $ch , CURLOPT_HEADER ,
FALSE ) ; curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST ,
0 ) ; curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER ,
0 ) ; $response = curl_exec ( $ch ) ; curl_close ( $ch ) ; print_r ( $response ) ;
Copy <? php
$ch = curl_init () ;
curl_setopt ( $ch , CURLOPT_URL , "http://httpbin.org/ip" ) ;
curl_setopt ( $ch , CURLOPT_PROXY , "http://scraperapi.render=true:APIKEY@proxy-server.scraperapi.com:8001" ) ;
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , TRUE ) ;
curl_setopt ( $ch , CURLOPT_HEADER , FALSE ) ;
curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST , 0 ) ;
curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER , 0 ) ;
$response = curl_exec ( $ch ) ;
curl_close ( $ch ) ;
var_dump ( $response ) ;
Copy # remember to install the library: composer require scraperapi/sdk
<? php
$client = new ScraperAPIClient ( "APIKEY" );
$result = $client -> get ( "http://httpbin.org/ip" , [ "render" => true ] ) -> raw_body;
print ( $result ) ;
Pass the parameter in the headers :
Copy <? php
$url = "https://api.scraperapi.com?url=https://httpbin.org/ip" ;
$ch = curl_init () ;
$headers = array (
'x-sapi-render: true' ,
'x-sapi-api_key: <YOUR_API_KEY>'
);
curl_setopt ( $ch , CURLOPT_URL , $url ) ;
curl_setopt ( $ch , CURLOPT_HTTPHEADER , $headers ) ;
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , TRUE ) ;
curl_setopt ( $ch , CURLOPT_HEADER , FALSE ) ;
curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST , 0 ) ;
curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER , 0 ) ;
$response = curl_exec ( $ch ) ;
curl_close ( $ch ) ;
print_r ( $response ) ;
?>
Copy <? php
$ch = curl_init () ;
curl_setopt ( $ch , CURLOPT_URL , "http://httpbin.org/ip" ) ;
curl_setopt ( $ch , CURLOPT_PROXY , "http://scraperapi:<YOUR_API_KEY>@proxy-server.scraperapi.com:8001" ) ;
$headers = array ( 'x-sapi-render: true' );
curl_setopt ( $ch , CURLOPT_HTTPHEADER , $headers ) ;
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , TRUE ) ;
curl_setopt ( $ch , CURLOPT_HEADER , FALSE ) ;
curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST , 0 ) ;
curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER , 0 ) ;
$response = curl_exec ( $ch ) ;
curl_close ( $ch ) ;
print_r ( $response ) ;
?>
Copy # remember to install the library: composer require scraperapi/sdk
<? php
$client = new ScraperAPIClient ( "APIKEY" );
$result = $client -> get ( "http://httpbin.org/ip" , [
'headers' => [ 'x-sapi-render' => 'true' ]] ) -> raw_body;
print ( $result ) ;
?>
Last updated 5 months ago