Skip to main content

Features of templates in A-Parser

Templates testing

For debugging and testing templates, A-Parser has a special tool: Templates testing

The .format method for arrays

In A-Parser, most results are presented as arrays with nested elements. In technical terms, results are represented as an array of hashes, where each hash has fixed keys. Let's look at this using the SE::GoogleSE::Google parser as an example; its results contain a $serp array with $link, $anchor, and $snippet elements (among others):

"serp" : [
{
"link" : "http://www.speedtest.net/",
"anchor" : "Speedtest.net by Ookla - The Global Broadband Speed <b>Test</b>",
"snippet" : "<b>Test</b> your Internet connection bandwidth to locations around the world with this <br>interactive broadband speed <b>test</b> from Ookla."
},
{
"link" : "http://www.speakeasy.net/speedtest/",
"anchor" : "Speakeasy Speed <b>Test</b>",
"snippet" : "Speakeasy Speed <b>Test</b> - Broadband Speed <b>Test</b>. Go to MegaPath Speed <b>Test</b> ... <br>02:38:36 PM Your IP: The Speakeasy Speed <b>Test</b> requires Flash v7 or higher."
},
{
"link" : "http://en.wikipedia.org/wiki/Test_cricket",
"anchor" : "<b>Test</b> cricket - Wikipedia, the free encyclopedia",
"snippet" : "<b>Test</b> cricket is the longest form of the sport of cricket. <b>Test</b> matches are played <br>between national representative teams with &quot;<b>Test</b> status&quot;, as determined by the&nbsp;..."
}
]

For convenient traversal and output of data from such an array, the .format method was created, which allows joining all array elements according to a specific format, for example, all links via a newline:

$serp.format('$link\n')

Which will result in saving each link on a new line:

http://www.speedtest.net/
http://www.speakeasy.net/speedtest/
http://en.wikipedia.org/wiki/Test_cricket

Snippet output:

$serp.format('$snippet\n')
<b>Test</b> your Internet connection bandwidth to locations around the world with this <br>interactive broadband speed <b>test</b> from Ookla.
Speakeasy Speed <b>Test</b> - Broadband Speed <b>Test</b>. Go to MegaPath Speed <b>Test</b> ...<br>02:38:36 PM Your IP: The Speakeasy Speed <b>Test</b> requires Flash v7 or higher.
<b>Test</b> cricket is the longest form of the sport of cricket. <b>Test</b> matches are played <br>between national representative teams with &quot;<b>Test</b> status&quot;, as determined by the&nbsp;...

Links, anchors, and snippets simultaneously:

$serp.format('Link: $link, Anchor: $anchor, Snippet: $snippet\n')
Link: http://www.speedtest.net/, Anchor: Speedtest.net by Ookla - The Global Broadband Speed <b>Test</b>, Snippet: <b>Test</b> your Internet connection bandwidth to locations around the world with this <br>interactive broadband speed <b>test</b> from Ookla.Link: http://www.speedtest.net/, Anchor: Speedtest.net by Ookla - The Global Broadband Speed <b>Test</b>, Snippet: <b>Test</b> your Internet connection bandwidth to locations around the world with this <br>interactive broadband speed <b>test</b> from Ookla.
Link: http://www.speakeasy.net/speedtest/, Anchor: Speakeasy Speed <b>Test</b>, Snippet: Speakeasy Speed <b>Test</b> - Broadband Speed <b>Test</b>. Go to MegaPath Speed <b>Test</b> ... <br>02:38:36 PM Your IP: The Speakeasy Speed <b>Test</b> requires Flash v7 or higher.Link: http://www.speakeasy.net/speedtest/, Anchor: Speakeasy Speed <b>Test</b>, Snippet: Speakeasy Speed <b>Test</b> - Broadband Speed <b>Test</b>. Go to MegaPath Speed <b>Test</b> ... <br>02:38:36 PM Your IP: The Speakeasy Speed <b>Test</b> requires Flash v7 or higher.
Link: http://en.wikipedia.org/wiki/Test_cricket, Anchor: <b>Test</b> cricket - Wikipedia, the free encyclopedia, Snippet: <b>Test</b> cricket is the longest form of the sport of cricket. <b>Test</b> matches are played <br>between national representative teams with &quot;<b>Test</b> status&quot;, as determined by the&nbsp;...Link: http://en.wikipedia.org/wiki/Test_cricket, Anchor: <b>Test</b> cricket - Wikipedia, the free encyclopedia, Snippet: <b>Test</b> cricket is the longest form of the sport of cricket. <b>Test</b> matches are played <br>between national representative teams with &quot;<b>Test</b> status&quot;, as determined by the&nbsp;...

The format can also use the original query (or other available variables), which allows setting a query - each array element mapping:

$serp.format('$query: $link\n')
test: http://www.speedtest.net/
test: http://www.speakeasy.net/speedtest/
test: http://en.wikipedia.org/wiki/Test_cricket

The .json method for objects

As known, all data in A-Parser is represented as variables. There is a serialization method (conversion to String type) for such data into JSON format: .json. For example:

$results.json
Example

Static template flag in Result file name

The isStaticTemplate() flag allows making a dynamic template in the Result file name static. Principle of operation: when using this flag in the Result file name, the template will execute once at the task start and will thus be considered static. This allows for more flexible file naming while maintaining the ability to get links to them via the getTaskResultsFile API method.

Usage example:

[% isStaticTemplate(); tools.js.eval('Date.now()') %]

Available variables

When formatting queries

When formatting results

When forming the result file name

When filtering results

Variable interpolation

By default, templates are written between [% and %] tags; everything outside the tags is plain text that will be passed to the result as is. A-Parser additionally includes variable interpolation, which allows accessing variables in text via the $ symbol. Besides this, \n is also interpolated as an explicit line break.

Example:

Total results for query $query: $totalcount\n

The values of the corresponding variables will be substituted in place of $query and $totalcount, and \n will be replaced by a line break. Equivalent notation without using interpolation:

Total results for query [% query %]: [% totalcount; "\n" %]
note

Please note that in Template Toolkit templates, variables are written without the $ prefix.

Template use cases

Query formatting

Example

In this example, the search operator site: will be added to each domain from the Alexa top500.txt file, and substitutions from the words.txt file will be added after a space.

Result formatting

Template in result format

In this example, the query, the number of results in the SERP, and the number of related keywords will be output. Also, a list of collected anchors will be displayed.

Templates in result filtering

Filtering template

To be able to set a template, you should select Custom Template in the dropdown list.
In this example, only those queries for which fewer than 5 results were collected will be output as the result.

Templates when using the Use regex option

Templates with regex using

In this example, the parser will collect sentences that contain the word passed as the second argument of the query. The algorithm is as follows: the query is split by the Query Builder using the specified delimiter into a link and a word; the parser follows the link, selects the text; the word from the query is substituted into the regular expression, and sentences are collected with it.

Download example

How to import an example into A-Parser

eJyNVE1T2zAQ/StUEwYo1EkoDK0vncA003YCoSScHLejxmtXjWwZSYZkQv57d2XH
diiHXmTp6b3d1X54zSw3C3OrwYA1zA/WLHd75rMIYl5Iy05YzrUBTdcB+zK9Hvn+
FJb289JqPrdKI6Pmrpld5YDqeWGsSu/AlCZ0ufEDZlFqEKEvC+kmgSUKDoNZ0Tvr
997R5zQOgx/em0+zWTab6fA42N97KECvvAWs9vZ37t4GuA+Pj1hlalr6F0nttUK4
1nyFoPve8JQwY7XIElNT6Y0VyMJNGNb4UOmUU1Y6ed+rVF7swMODTglgQAdHjakJ
f4SpQkksJDTwEE+V907ELdDt1tKRZ5eULR5FwgqVcVn6pbCaWO4zgclAfaaQS3kR
YIZapQi5rJbgahtzwDruTNkunPZ7qWF+zKWBE2Yw1CHHQKKXN8KC5ljjcU7xIL5m
KhtIOYJHkA3N2b8shIywTwYxir5Wwtcp439sbOrntV09gn7SGENtxZ0ux9eNKlIj
lWyTIUUqLJ7NlSoyKlcPwQVAXufshmip0lC7qSxX3nEUcsgiZDYlG+QNtPMMNzBG
FXpOpsskn2wnoGyKSS4FVcQATlGZEPZMJEWF2Uqwranfao8tBwjOVRaLZIzJ0CKC
bTMU2RRnd5xdqTSXQDnKCimxxAbumlYbmKqkdGge+1J85VzsTL1VSppvk/LZuRYY
8DkFmGJV2l4rk3Mu5f3dqH3DmvbEw29rc+N3u7rwnsRC5BAJ7imddOnUdVPfp/X9
B7dGtJ6dun3PrdwhLc7Zhduft3DesvDxZ0sctfa/WqSL5/8hMXqfhURhtTD7VKnq
p1j/Otev/hr99QZ79I+5LdlUBOIihtU02IDM72/+Ar1q4iA=

Templates in parser settings

Example of random user-agent substitution

Preset macros settings

In A-Parser, you can configure template macros and preset variables that will be available globally for all templates; global macros can be specified in Settings -> Additional settings.

By default, it already contains the predefinition of the $datefile object, which is used for time formatting for the result file name.

Adding and using a macro

This example shows the setting of a global variable. This can be useful, for example, if you need to use the same cookies in several Instagram parsers.

Installation example:

Example
caution

Note the syntax of the closing template bracket -%]. This is needed to remove the line break; otherwise, an empty line will be added at the beginning when using any template.

Usage example:

Example