WebScrapingAPI Docs

Extraction Rules

Customize your response by adding extraction rules.

Extraction rules can be applied with both JavaScript rendering enabled and disabled.

Browser API allows you to extract specific sections of the webpage. You can do so by using the extract_rules parameter.

This parameter's value can be a string (the CSS selector or XPath) or a stringified object. In the second case, the parameter accepts the following options:

ParameterTypeRequiredDescription
selectorstring✅The CSS selector or the XPath.
selector_typestring❌The type of the selector option. Accepted values are css and xpath. The default value is xpath if the selector option starts with /, and css otherwise.
outputstring❌The output format of the selected element. Accepted values are html, text, @[attr], table_json, table_array, or another extract_rules object for nested parsing.
allint❌Returns all possible elements. The default value for this parameter is "1".
cleanint❌Removes leading and trailing white spaces, line terminator characters, and newlines from the result. The default value for this parameter is "1".

A full example of how this parameter would look in production is:

extract_rules='{"title": {"selector": "h1", "output": "html"}, "subtitle": {"selector": ".font-light.max-w-6xl", "output": "text"}}'

or:

extract_rules='{"title": "h1"}'

Extraction Rules Integration Examples

Extract Content Based on CSS Rules

The following examples show how the extract_rules parameter is used in order to extract specific elements from the targeted website.

Query Parameters

NameTypeDescription
api_keystring<YOUR_API_KEY>
urlstringhttps://webscrapingapi.com
extract_rulesobject{"title":{"selector":"h1","output":"html"},"subtitle":{"selector":".font-light.max-w-6xl","output":"text"}}
{
    "title": [
        "<h1 class=\"max-w-2xl mb-4 font-extrabold tracking-tight leading-tight dark:text-white text-4xl md:text-5xl xl:text-6xl\">Transform Websites <br />Into Data</h1>"
    ],
    "subtitle": [
        "Navigate the web data landscape effortlessly with our proxy networks, cutting-edge web scrapers, and dedicated data extraction experts. Choose your path - DIY or fully managed by us.",
        "Data collection reinvented: with our advanced scraper APIs, infrastructure building and maintenance become a thing of the past.",
        "Empower your business decision-making with our reliable managed data extraction service. We handle the intricate extraction process, legal compliance, and quality assurance so you can focus on deriving insights.",
        "Unleash the power of insight with tailored data solutions to push your business forward.",
        "Elevate your competitive game in your Industry",
        "From startups to Fortune 500s, WebScrapingAPI® stands as the premier choice for progressive businesses seeking superior data gathering solutions worldwide.",
        "Effortless and innovative solutions tailored to your unique use case, just a click away."
    ]
}
{
    "status": "Failure",
    "status_code": 400,
    "created_at": "2022-09-12T12:09:55.157Z",
    "processed_at": null,
    "time_taken": {
        "total": 0.034,
        "scraping": 0
    },
    "error": "Key `extract_rules` must be a stringified object."
}

The full GET request for the extract_rules should be:

https://api.webscrapingapi.com/v1?api_key=<YOUR_API_KEY>&url=https://webscrapingapi.com&extract_rules=%7B%22title%22%3A%20%7B%22selector%22%3A%20%22h1%22%2C%20%22output%22%3A%20%22text%22%7D%2C%20%22subtitle%22%3A%20%7B%22selector%22%3A%20%22.font-light.max-w-6xl%22%2C%20%22output%22%3A%20%22text%22%7D%7D
curl https://api.webscrapingapi.com/v1?api_key=<YOUR_API_KEY>&url=https%3A%2F%2Fwebscrapingapi.com&extract_rules=%7B%22title%22%3A%20%7B%22selector%22%3A%20%22h1%22%2C%20%22output%22%3A%20%22text%22%7D%2C%20%22subtitle%22%3A%20%7B%22selector%22%3A%20%22.font-light.max-w-6xl%22%2C%20%22output%22%3A%20%22text%22%7D%7D
const response = await fetch("https://api.webscrapingapi.com/v1?api_key=<YOUR_API_KEY>&url=https%3A%2F%2Fwebscrapingapi.com&extract_rules=%7B%22title%22%3A%20%7B%22selector%22%3A%20%22h1%22%2C%20%22output%22%3A%20%22text%22%7D%2C%20%22subtitle%22%3A%20%7B%22selector%22%3A%20%22.font-light.max-w-6xl%22%2C%20%22output%22%3A%20%22text%22%7D%7D", {
  method: "GET"
});

const data = await response.text();
console.log(data);
import requests

API_KEY = '<YOUR_API_KEY>'
SCRAPER_URL = 'https://api.webscrapingapi.com/v1'

TARGET_URL = 'https://webscrapingapi.com/'

PARAMS = {
    "api_key":API_KEY,
    "url": TARGET_URL,
    "extract_rules":'{"title": {"selector": "h1", "output": "html"}, "subtitle": {"selector": ".font-light.max-w-6xl", "output": "text"}}'
}

response = requests.get(SCRAPER_URL, params=PARAMS)

print(response.text)
<?php

$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "https://api.webscrapingapi.com/v1?api_key=<YOUR_API_KEY>&url=https%3A%2F%2Fwebscrapingapi.com&extract_rules=%7B%22title%22%3A%20%7B%22selector%22%3A%20%22h1%22%2C%20%22output%22%3A%20%22text%22%7D%2C%20%22subtitle%22%3A%20%7B%22selector%22%3A%20%22.font-light.max-w-6xl%22%2C%20%22output%22%3A%20%22text%22%7D%7D",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
package main

import (
	"fmt"
	"net/http"
	"io/ioutil"
)

func main() {

	url := "https://api.webscrapingapi.com/v1?api_key=<YOUR_API_KEY>&url=https%3A%2F%2Fwebscrapingapi.com&extract_rules=%7B%22title%22%3A%20%7B%22selector%22%3A%20%22h1%22%2C%20%22output%22%3A%20%22text%22%7D%2C%20%22subtitle%22%3A%20%7B%22selector%22%3A%.font-light.max-w-6xl%22%2C%20%22output%22%3A%20%22text%22%7D%7D"

	req, _ := http.NewRequest("GET", url, nil)

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
HttpResponse<String> response = Unirest.get("https://api.webscrapingapi.com/v1?api_key=<YOUR_API_KEY>&url=https%3A%2F%2Fwebscrapingapi.com&extract_rules=%7B%22title%22%3A%20%7B%22selector%22%3A%20%22h1%22%2C%20%22output%22%3A%20%22text%22%7D%2C%20%22subtitle%22%3A%20%7B%22selector%22%3A%20%22.font-light.max-w-6xl%22%2C%20%22output%22%3A%20%22text%22%7D%7D")
  .asString();
var client = new RestClient("https://api.webscrapingapi.com/v1?api_key=<YOUR_API_KEY>&url=https%3A%2F%2Fwebscrapingapi.com&extract_rules=%7B%22title%22%3A%20%7B%22selector%22%3A%20%22h1%22%2C%20%22output%22%3A%20%22text%22%7D%2C%20%22subtitle%22%3A%20%7B%22selector%22%3A%20%22.font-light.max-w-6xl%22%2C%20%22output%22%3A%20%22text%22%7D%7D");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
require 'uri'
require 'net/http'
require 'openssl'

url = URI("https://api.webscrapingapi.com/v1?api_key=<YOUR_API_KEY>&url=https%3A%2F%2Fwebscrapingapi.com&extract_rules=%7B%22title%22%3A%20%7B%22selector%22%3A%20%22h1%22%2C%20%22output%22%3A%20%22text%22%7D%2C%20%22subtitle%22%3A%20%7B%22selector%22%3A%20%22.font-light.max-w-6xl%22%2C%20%22output%22%3A%20%22text%22%7D%7D")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Get.new(url)

response = http.request(request)
puts response.read_body

Important! The url & extract_rules parameters have to be encoded. (i.e. &url=https%3A%2F%2Fwww.webscrapingapi.com%2F&extract_rules=%7B%22title%22%3A%20%7B%22selector...)

Response example

{
    "title": [
        "Transform Websites Into Data"
    ],
    "subtitle": [
        "Navigate the web data landscape effortlessly with our proxy networks, cutting-edge web scrapers, and dedicated data extraction experts. Choose your path - DIY or fully managed by us.",
        "Data collection reinvented: with our advanced scraper APIs, infrastructure building and maintenance become a thing of the past.",
        "Empower your business decision-making with our reliable managed data extraction service. We handle the intricate extraction process, legal compliance, and quality assurance so you can focus on deriving insights.",
        "Unleash the power of insight with tailored data solutions to push your business forward.",
        "Elevate your competitive game in your Industry",
        "From startups to Fortune 500s, WebScrapingAPI® stands as the premier choice for progressive businesses seeking superior data gathering solutions worldwide.",
        "Effortless and innovative solutions tailored to your unique use case, just a click away."
    ]
}

More extract_rules object examples

Here are more examples that should help you better understand how the object passed to the extract_rules parameter should look like:

Text Content

Return the text content of elements with the CSS class .title.

HTML sample

<div class="title">
  This is my title
</div>

Extraction rule

{
  "title": ".title"
}

JSON output

{
  "title": [
    "This is my title"
  ]
}

Return the href attribute of all links on the page.

HTML sample

<div>
  <a href="https://www.webscrapingapi.com/product/">Product</a>
  <a href="https://www.webscrapingapi.com/pricing/">Pricing</a>
</div>

Extraction rule

{
  "links": {
    "selector": "a",
    "output": "@href",
    "all": "1"
  }
}

JSON output

{
  "links": [
    "https://www.webscrapingapi.com/product/",
    "https://www.webscrapingapi.com/pricing/"
  ]
}

Image Attributes

Return the src attribute of the first image available on the page.

HTML sample

<div>
  <img src="https://www.webscrapingapi.com/assets/images/icons/full.svg?v=41d081a6f0" />
</div>

Extraction rule

{
  "image": {
    "selector": "img",
    "output": "@src",
    "all": 0
  }
}

JSON output

{
  "image": [
    "https://www.webscrapingapi.com/assets/images/icons/full.svg?v=41d081a6f0"
  ]
}

Table JSON

Return the first table with the CSS class .ants as JSON.

HTML sample

<table class="ants">
  <thead>
    <tr>
      <th>Region</th>
      <th>No. species</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Europe</td>
      <td>180</td>
    </tr>
  </tbody>
</table>

Extraction rule

{
  "table": {
    "selector": ".ants",
    "output": "table_json",
    "all": 0
  }
}

JSON output

{
  "table": [
    {
      "Region": "Europe",
      "No. species": "180"
    }
  ]
}

Table Array

Return the first table with the CSS class .ants as an array.

HTML sample

<table class="ants">
  <thead>
    <tr>
      <th>Region</th>
      <th>No. species</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Europe</td>
      <td>180</td>
    </tr>
  </tbody>
</table>

Extraction rule

{
  "table": {
    "selector": ".ants",
    "output": "table_array",
    "all": 0
  }
}

JSON output

{
  "table": [
    ["Europe", "180"]
  ]
}

Nested Elements

Return the name and price for each list item.

HTML sample

<ul>
  <li>
    <p class="name">Item1</p>
    <p class="price">100</p>
  </li>
  <li>
    <p class="name">Item2</p>
    <p class="price">1000</p>
  </li>
</ul>

Extraction rule

{
  "items": {
    "selector": "li",
    "output": {
      "name": {
        "selector": ".name",
        "all": 0
      },
      "price": {
        "selector": ".price",
        "all": 0
      }
    },
    "all": 1
  }
}

JSON output

{
  "items": [
    {
      "name": "Item1",
      "price": "100"
    },
    {
      "name": "Item2",
      "price": "1000"
    }
  ]
}

On this page