-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compare Product Prices Example (#78)
* Update * Update * Update * Update * Update * Update * Update * Update --------- Co-authored-by: R L Nabors <rachelnabors@users.noreply.github.com>
- Loading branch information
1 parent
2b5f1b4
commit 2bb7af2
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Example script: comparing product price across websites with AgentQL | ||
|
||
This example demonstrates how to compare product prices across websites with queryData() method. | ||
|
||
## Run the script | ||
|
||
- [Install AgentQL SDK](https://docs.agentql.com/javascript-sdk/installation) | ||
- Save this JavaScript file locally as **compare-product-prices.js** | ||
- Run the following command from the project's folder: | ||
|
||
```bash | ||
node compare-product-prices.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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const { wrap } = require('agentql'); | ||
const { chromium } = require('playwright'); | ||
|
||
// Set the URL to the desired website | ||
const BESTBUY_URL = 'https://www.bestbuy.com'; | ||
const EBAY_URL = 'https://www.ebay.com'; | ||
const TELQUEST_URL = 'https://www.telquestintl.com'; | ||
|
||
// Define the queries to interact with the page | ||
const HOME_PAGE_QUERY = ` | ||
{ | ||
search_input | ||
search_button | ||
} | ||
`; | ||
|
||
const PRODUCT_INFO_QUERY = ` | ||
{ | ||
product_price (for Nintendo Switch - OLed Model - w/ White Joy-Con) | ||
} | ||
`; | ||
|
||
/** | ||
* Open the given URL in a new tab and fetch the price of the product. | ||
*/ | ||
async function fetchPrice(context, sessionUrl) { | ||
// Create a page in a new tab in the broswer context and wrap it to get access to the AgentQL's querying API | ||
const page = wrap(await context.newPage()); | ||
await page.goto(sessionUrl); | ||
|
||
// Search for the product | ||
await page.waitForLoadState('networkidle'); | ||
const homeResponse = await page.queryElements(HOME_PAGE_QUERY); | ||
await homeResponse.search_input.fill('Nintendo Switch - OLED Model White'); | ||
await homeResponse.search_button.click(); | ||
|
||
// Fetch the price data from the page | ||
const data = await page.queryData(PRODUCT_INFO_QUERY); | ||
return data.product_price; | ||
} | ||
|
||
/** | ||
* Fetch prices concurrently in the same browser session from multiple websites. | ||
*/ | ||
async function getPriceAcrossWebsites() { | ||
const browser = await chromium.launch({ headless: false }); | ||
const context = await browser.newContext(); | ||
|
||
// Open multiple tabs in the same browser context to fetch prices concurrently | ||
const [bestbuyPrice, ebayPrice, telquestPrice] = await Promise.all([ | ||
fetchPrice(context, BESTBUY_URL), | ||
fetchPrice(context, EBAY_URL), | ||
fetchPrice(context, TELQUEST_URL), | ||
]); | ||
|
||
console.log(` | ||
Price at BestBuy: ${bestbuyPrice} | ||
Price at eBay: ${ebayPrice} | ||
Price at Telquest: ${telquestPrice} | ||
`); | ||
|
||
await browser.close(); | ||
} | ||
|
||
getPriceAcrossWebsites(); |