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.
Copy try {
String apiKey = "APIKEY" ;
String url = "http://api.scraperapi.com?api_key=" + apiKey + "&url=http://httpbin.org/ip&render=true" ;
URL urlForGetRequest = new URL(url) ;
String readLine = null ;
HttpURLConnection conection = (HttpURLConnection) urlForGetRequest . openConnection ();
conection . setRequestMethod ( "GET" );
int responseCode = conection . getResponseCode ();
if (responseCode == HttpURLConnection . HTTP_OK ) {
BufferedReader in = new BufferedReader( new InputStreamReader( conection . getInputStream())) ;
StringBuffer response = new StringBuffer() ;
while ((readLine = in . readLine ()) != null ) {
response . append (readLine);
}
in . close ();
System . out . println ( response . toString ());
} else {
throw new Exception( "Error in API Call" ) ;
}
} catch ( Exception ex) {
ex . printStackTrace ();
}
Copy try {
String apiKey = "APIKEY" ;
String proxy = "http://scraperapi.render=true:" + apiKey + "@proxy-server.scraperapi.com" ;
URL server = new URL( "http://httpbin.org/ip" ) ;
Properties systemProperties = System . getProperties ();
systemProperties . setProperty ( "http.proxyHost" , proxy);
systemProperties . setProperty ( "http.proxyPort" , "8001" );
HttpURLConnection httpURLConnection = (HttpURLConnection) server . openConnection ();
httpURLConnection . connect ();
String readLine = null ;
int responseCode = httpURLConnection . getResponseCode ();
if (responseCode == HttpURLConnection . HTTP_OK ) {
BufferedReader in = new BufferedReader( new InputStreamReader( httpURLConnection . getInputStream())) ;
StringBuffer response = new StringBuffer() ;
while ((readLine = in . readLine ()) != null ) {
response . append (readLine);
}
in . close ();
System . out . println ( response . toString ());
} else {
throw new Exception( "Error in API Call" ) ;
}
} catch ( Exception ex) {
ex . printStackTrace ();
}
Copy // remember to install the library: https://search.maven.org/artifact/com.scraperapi/sdk/1.0
ScraperApiClient client = new ScraperApiClient( "APIKEY" ) ;
client . get ( "http://httpbin.org/ip" )
. render ( true )
. result ();