Scraping Dog
Data AccessIntroducing Scrapingdog API
Scrapingdog is a powerful web scraping API that enables you to extract data from any website on the internet. It supports a wide range of features such as dynamic rendering, rotating proxies, headless browsing, and HTML parsing. In this blog, we will cover some examples of how you can utilize Scrapingdog API in JavaScript.
Getting Started
Before we dive into the examples, we need to get our API key from Scrapingdog. To achieve that, follow these simple steps:
- Go to https://www.scrapingdog.com/.
- Click on the "Sign up" button.
- Fill in the necessary details and proceed to sign up.
- Once you are logged in, navigate to the dashboard and copy your API key.
Example 1: Scraping a static website
Let's start with the simplest example. In this example, we will extract the title of a static website using Scrapingdog API and display it in the console.
const apiKey = "YOUR_API_KEY_HERE";
const url = "https://example.com";
const headers = {
"Content-Type": "application/json",
"API-KEY": apiKey,
};
const body = JSON.stringify({ url });
fetch("https://api.scrapingdog.com/scrape", {
method: "POST",
headers,
body,
})
.then((response) => response.json())
.then((data) => console.log(data.title));
Explanation:
- We define
apiKey
,url
andheaders
as constants to identify the API key, the URL to be scraped, and the headers for our request, respectively. - We create a
body
object which includes theurl
and converts to JSON format. - We send an HTTP POST request using the
fetch
method with the API endpoint andheaders
. - Once we receive the response, we extract the
title
field from the response object and display it in the console.
Example 2: Scraping a dynamic website
In this example, we will scrape data from a dynamic website that loads data dynamically via JavaScript. In order to do this, we need to use the dynamic rendering feature.
const apiKey = "YOUR_API_KEY_HERE";
const url = "https://example.com";
const headers = {
"Content-Type": "application/json",
"API-KEY": apiKey,
};
const body = JSON.stringify({
url,
render_js: true,
wait: 5000, // wait 5 seconds for page to load
});
fetch("https://api.scrapingdog.com/scrape", {
method: "POST",
headers,
body,
})
.then((response) => response.json())
.then((data) => console.log(data));
Explanation:
- We define
apiKey
,url
andheaders
as constants to identify the API key, the URL to be scraped, and the headers for our request, respectively. - We create a
body
object which includes theurl
,render_js
set totrue
andwait
for 5 seconds for the page to load. - We send an HTTP POST request using the
fetch
method with the API endpoint andheaders
. - Once we receive the response, we extract the necessary fields from the response object and display them in the console.
Example 3: Scraping data from a search engine
In this example, we will scrape data from a search engine and display the first 10 results.
const apiKey = "YOUR_API_KEY_HERE";
const url = "https://www.google.com/search?q=kittens";
const headers = {
"Content-Type": "application/json",
"API-KEY": apiKey,
};
const body = JSON.stringify({
url,
render_js: true,
});
fetch("https://api.scrapingdog.com/scrape", {
method: "POST",
headers,
body,
})
.then((response) => response.json())
.then((data) => {
const results = data.search_results;
for (let i = 0; i < 10; i++) {
console.log(results[i].title);
console.log(results[i].link);
}
});
Explanation:
- We define
apiKey
,url
andheaders
as constants to identify the API key, the URL to be scraped, and the headers for our request, respectively. - We create a
body
object which includes theurl
andrender_js
set totrue
. - We send an HTTP POST request using the
fetch
method with the API endpoint andheaders
. - Once we receive the response, we extract the
search_results
field from the response object and loop through the first 10 elements to display thetitle
andlink
fields in the console.
Conclusion
Scrapingdog API provides a straightforward and easy-to-use interface for website scraping. With its powerful features and support for JavaScript, you can easily extract data from any website on the internet. We hope these examples give you an idea of how to use Scrapingdog API, and we encourage you to explore its full potential. Happy scraping!