Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // import the required library
- const puppeteer = require('puppeteer');
- const url = 'https://www.amazon.com/s?k=headphones';
- (async () => {
- const browser = await puppeteer.launch({ headless: 'new' });
- const page = await browser.newPage();
- // intercept requests to modify them
- await page.setRequestInterception(true);
- page.on('request', (request) => {
- // modify the request to include params
- const urlWithParams = new URL(request.url());
- urlWithParams.searchParams.append('url', url);
- urlWithParams.searchParams.append('apikey', '<YOUR_ZENROWS_API_KEY>');
- request.continue({ url: urlWithParams.toString() });
- });
- try {
- // navigate to the modified URL
- const response = await page.goto(url);
- // check if the response is successful (status code 200)
- if (response && response.status() === 200) {
- // extract data from the page
- const extractedData = await page.evaluate(() => {
- return {
- // extract title text to further confirm access
- title: document.querySelector('h1').textContent,
- };
- });
- console.log(extractedData);
- } else {
- console.error(`Failed to load the page. Status: ${response ? response.status() : 'unknown'}`);
- }
- } catch (error) {
- console.error('Error:', error);
- } finally {
- await browser.close();
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement