Getting started
Last updated
Last updated
To use Custom Parser, please provide a set of parsing_instructions
when creating a job.
Let's say you want to parse the number of total results Bing Search yields with a search term test
:
An example job parameters would look as follows:
Step 1. You must provide the "parse": true
parameter.
Step 2. Parsing instructions should be described in the "parsing_instructions"
field.
The sample parsing instructions above specifies that the aim is to parse the number of search results from the scraped document and put the result in the number_of_results
field. The instructions on how to parse the field by defining a “pipeline” is given:
The pipeline describes a list of data processing functions to be executed. The functions are executed in the order they appear on the list and take the output of the previous function as the input.
In the sample pipeline above, the xpath_one
function (full list of available functions) is used. It allows you to process an HTML document using XPath expressions and XSLT functions. As a function argument, specify the exact path where the target element can be found: .//span[@class='sb_count']
. You can also instruct the parser to select the text()
found in the target element.
The parsed result of the sample job above should look like this:
Custom Parser not only offers text extraction from a scraped HTML, but it can also execute basic data processing functions.
For example, the previously described parsing instructions extract number_of_results
as a text with extra keywords you may not necessarily need. If you want to get the number of results for the given query=test
in the numeric data type, you can reuse the same parsing instructions and add the amount_from_string
function to the existing pipeline:
The parsed result of the sample job above should look like this:
We can see that it parsed the document accurately:
When writing HTML element selection functions, make sure to work with scraped documents instead of live website version loaded on your browser, as the documents can differ. The main reason behind this issue is JavaScript rendering. When a website is opened, your browser is responsible for loading additional documents, such as CSS stylesheets and JavaScript scripts, which can change the structure of the initial HTML document. When parsing scraped HTMLs, Custom Parser does not load the HTML document the same way browsers do (parsers ignore JavaScript instructions), thus the HTML tree can differ between what the parser and browser render.
As an example, take a look at the following HTML document:
If you open the document via browser, it will show the price you can select using the following XPath expression //p[@id="price"]
:
Now if you disable JavaScript rendering in the browser, the website will render as follows:
The same //p[@id="price"]
XPath expression no longer matches the price as it is not rendered.
For various reasons, the same page scraped twice may have different layouts (different User Agents used when scraping, target website doing A/B testing, etc.).
To tackle this problem, we suggest defining parsing_instructions
for the initially scraped document and testing these instructions right away with multiple other scraped job results of the same page type.
HTML selector functions (xpath
/xpath_one
) support selector fallbacks.
Scrape the HTML document of the target page using Scraper API.
Disable JavaScript and open the scraped HTML locally on your browser. If JavaScript is disabled after the HTML is opened, make sure to reload the page so that the HTML can reload without JavaScript.
Let's say you have the following page to parse:
Create a new JSON object and assign a new field to it.
You can name the field any way you prefer with some exceptions (user-defined field name cannot start with an underscore _
, e.g., "_title"
).
The field name will be displayed in the parsed result.
The new field must hold a value of JSON object type:
If you provide these instructions to Custom Parser, it would do nothing or send a complaint that you haven’t provided any instructions.
To actually parse the title into the title
field, you must define a data processing pipeline inside of the title
object using the reserved _fns
property (which is always of array type):
For Custom Parser to select the text of the title, you can utilize the HTML selector function xpath_one
. To use the function on the HTML document, it should be added to the data processing pipeline. The function is defined as a JSON object with required _fn
(function name) and required _args
(function arguments) fields. See the full list of function definitions here.
The parsing instructions above should produce the following result:
Similarly, in parsing instructions, you can define another field where the product description container, description title, and items will be parsed. For the title and the items of the description to be nested under the description
object, the structure of instructions should be as follows:
The given structure of parsing instructions implies that description.title
and description.items
will be parsed based on description
element. You can define a pipeline for the description
field. In this case, it's done first as it will simplify the XPath expression of the title of the description.
In the example, the description._fns
pipeline will select the description-container
HTML element, which will be used as a reference point for parsing the description title and items.
To parse the remaining description fields, add two different pipelines for fields description.items
, and description.title
:
Notice how the xpath
function is used instead of xpath_one
to extract all items that match the XPath expression.
The parsing instructions produce the following result:
The following example shows the structure of instructions if you want to parse information into product_variants
field, which will contain a list of variant objects. In this case, the variant object has price
and color
fields.
Start with selecting all product variant elements:
To make product_variants
a list containing JSON objects, you will have to iterate through found variants using _items
iterator:
Lastly, define instructions on how to parse the color
and price
fields:
With product_variants
described, the final instructions will look as follows:
Which will produce the following output:
You can find more examples of parsing instructions here: Parsing instruction examples.
If Custom Parser fails to process client-defined parsing instructions, we will return the 12005 status code (parsed with warnings).
The client will be charged for such results:
If Custom Parser encounters an exception and breaks during the parsing operation, it can return these status codes: 12002
, 12006
, 12007
. You will not be charged for these unexpected errors.
See our status codes outlined here.