Skip to main content
Version: v2

Exporting Scripts

BrowserQL IDE allows you to export any of your queries into different languages. This feature makes your life easier when integrating our functionalities into your scraping application. This allows you to try and test your queries in our IDE, and quickly move a working one into your code.

Example

In this example, the BQL query will navigate to https://news.ycombinator.com/, click on the Jobs tab, and extract the HTML from the page:

mutation ExtractJobsHTML {
goto(url: "https://news.ycombinator.com", waitUntil: firstMeaningfulPaint) {
status
}

click(selector: "span.pagetop > a[href='jobs']") {
x
y
}

html {
html
}
}

Now, using BrowserQL Editor to export this script in your preferred coding language to integrate it with a application that reads the HTML and extracts the title and link from it, creating an array with this information.

const { JSDOM } = require("jsdom");

async function ExtractJobsHTML() {
const url = "https://production-sfo.browserless.io/chromium/bql?token=YOUR_API_TOKEN_HERE";
const options = {
method: 'POST',
body: '{"query":"mutation ExtractJobsHTML {\n goto(url: \"https://news.ycombinator.com\", waitUntil: firstMeaningfulPaint) {\n status\n }\n \n click(selector: \"span.pagetop > a[href=\'jobs\']\") {\n x\n y\n }\n \n html {\n html\n }\n}","variables":null,"operationName":"ExtractJobsHTML"}'
};

let response;

try {
response = await fetch(url, options);
} catch (error) {
console.error(error);
}

const json = await response.json();
const html = json.data.html.html;

const dom = new JSDOM(html);
const document = dom.window.document;
const rows = document.querySelectorAll("tr.athing");

const titlesAndLinks = [];
rows.forEach((row) => {
const anchor = row.querySelector("a");
if (anchor) {
titlesAndLinks.push({
title: anchor.textContent.trim(),
link: anchor.href.trim(),
});
}
});

console.log(titlesAndLinks);
}

ExtractJobsHTML();