ScraperAPI exposes a single API endpoint for you to send GET requests. Simply send a GET request to http://api.scraperapi.com
with two query string parameters and the API will return the HTML response for that URL:
api_key
which contains your API key, and
url
which contains the url you would like to scrape
You should format your requests to the API endpoint as follows:
Copy try {
String apiKey = "APIKEY" ;
String url = "https://api.scraperapi.com?api_key=" + apiKey + "&url=http://httpbin.org/ip" ;
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 ();
}
To enable other API functionality when sending a request to the API endpoint simply add the appropriate query parameters to the end of the ScraperAPI URL.
For example, if you want to enable Javascript rendering with a request, then add render=true
to the request:
Copy try {
String apiKey = "APIKEY" ;
String url = "https://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 ();
}
To use two or more parameters, simply separate them with the “&” sign.
Copy try {
String apiKey = "APIKEY" ;
String url = "https://api.scraperapi.com?api_key=" + apiKey + "&url=http://httpbin.org/ip&render=true&country_code=us" ;
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 ();
}
Last updated 3 months ago