import mql from '@microlink/mql' const { data } = await mql('https://kikobeats.com', { data: { avatar: { selector: "#avatar", type: "image", attr: "src" } } })
data
Type: <object>
It enables specific data extraction over the target url, turning Microlink API into a powerful web scraping tool.
The data extraction needs to be defined at least with a CSS selector.
Response structure
The extracted data will be part of the
data payload in the response:Rule properties
Each data rule accepts the following properties:
| Property | Type | Description |
|---|---|---|
| selector | string | CSS selector to target the element |
| selectorAll | string | CSS selector to target multiple elements |
| attr | string | HTML attribute to extract (e.g., href, src) |
| type | string | Data type for validation (string, number, date, image, url, etc.) |
| evaluate | function | JavaScript function to transform the extracted value |
Extracting multiple fields
You can extract multiple data fields in a single request:
import mql from '@microlink/mql' const { data } = await mql('https://news.ycombinator.com', { data: { headline: { selector: ".titleline > a", attr: "text" }, link: { selector: ".titleline > a", attr: "href", type: "url" }, score: { selector: ".score", attr: "text" } } })
Extract title, link, and score from Hacker News.
Extracting collections
Use selectorAll to extract multiple matching elements as an array:
import mql from '@microlink/mql' const { data } = await mql('https://news.ycombinator.com', { data: { stories: { selectorAll: ".titleline > a", attr: "text" } } })
Extract all story titles as an array.
Nested data extraction
For complex DOM structures, use nested rules to organize your data hierarchically:
import mql from '@microlink/mql' const { data } = await mql('https://github.com/microlinkhq', { data: { repos: { selectorAll: ".repo", data: { name: { selector: "a", attr: "text" }, description: { selector: "p", attr: "text" } } } } })
Fallback values
Define fallback rules to ensure data extraction succeeds even when the primary selector fails:
import mql from '@microlink/mql' const { data } = await mql('https://example.com', { data: { title: [ { selector: 'meta[property="og:title"]', attr: "content" }, { selector: "title", attr: "text" }, { selector: "h1", attr: "text" } ] } })
Try multiple selectors in order until one succeeds.
You can read the Microlink Query Language documentation to learn more about defining data extraction rules.