Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add js example for list query usage #83

Merged
merged 7 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions javascript-sdk/examples/list-query-usage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Example script: querying a list of items with AgentQL

This example demonstrates how to query a list of items on the page.

## Run the script

- [Install AgentQL SDK](https://docs.agentql.com/javascript-sdk/installation)
- Save this Javascript file locally as **list-query-usage.js**
- Run the following command from the project's folder:

```bash
node list-query-usage.js
```

## Play with the query

Install the [AgentQL Debugger Chrome extension](https://docs.agentql.com/installation/chrome-extension-installation) to play with the AgentQL query. [Learn more about the AgentQL query language](https://docs.agentql.com/agentql-query/query-intro)
34 changes: 34 additions & 0 deletions javascript-sdk/examples/list-query-usage/list-query-usage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { wrap } = require('agentql');
const { chromium } = require('playwright');
const path = require('path');
const fs = require('fs');

(async () => {
const browser = await chromium.launch({ headless: false });
const page = wrap(await browser.newPage()); // Wraps the Playwright Page to access AgentQL's features.

await page.goto('https://scrapeme.live/shop/');

const QUERY = `
{
products[]
{
product_name
price
}
}
`;

const response = await page.queryData(QUERY);

const scriptDir = path.dirname(__filename);
const csvFilePath = path.join(scriptDir, 'products_data.csv');
let csvContent = 'Products Name, Price\n';

response.products.forEach((product) => {
csvContent += `${product.product_name},${product.price}\n`;
});

fs.writeFileSync(csvFilePath, csvContent, 'utf-8');
await browser.close();
})();
17 changes: 17 additions & 0 deletions javascript-sdk/examples/list-query-usage/products_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Products Name, Price
Bulbasaur,£63.00
Ivysaur,£87.00
Venusaur,£105.00
Charmander,£48.00
Charmeleon,£165.00
Charizard,£156.00
Squirtle,£130.00
Wartortle,£123.00
Blastoise,£76.00
Caterpie,£73.00
Metapod,£148.00
Butterfree,£162.00
Weedle,£25.00
Kakuna,£148.00
Beedrill,£168.00
Pidgey,£159.00
Loading