-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding puppeteer timeout and fixing waitForSelector
- Loading branch information
Christian Kellner
authored and
Christian Kellner
committed
Jan 7, 2025
1 parent
26127ee
commit e1db384
Showing
4 changed files
with
42 additions
and
40 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
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
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
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 |
---|---|---|
@@ -1,48 +1,49 @@ | ||
import puppeteer from 'puppeteer-extra'; | ||
import StealthPlugin from 'puppeteer-extra-plugin-stealth'; | ||
import {debug, DEFAULT_HEADER, botDetected} from './utils.js'; | ||
import { debug, DEFAULT_HEADER, botDetected } from './utils.js'; | ||
|
||
puppeteer.use(StealthPlugin()); | ||
|
||
export default async function execute(url, waitForSelector, options) { | ||
let browser; | ||
try { | ||
debug(`Sending request to ${url} using Puppeteer.`); | ||
let browser; | ||
try { | ||
debug(`Sending request to ${url} using Puppeteer.`); | ||
|
||
browser = await puppeteer.launch({ | ||
headless: options.puppeteerHeadless ?? true, | ||
args: ['--no-sandbox', '--disable-gpu', '--disable-setuid-sandbox'] | ||
}); | ||
let page = await browser.newPage(); | ||
await page.setExtraHTTPHeaders(DEFAULT_HEADER); | ||
const response = await page.goto(url, { | ||
waitUntil: 'domcontentloaded' | ||
}); | ||
let pageSource; | ||
//if we're extracting data from a spa, we must wait for the selector | ||
if (waitForSelector != null) { | ||
await page.waitForSelector(waitForSelector); | ||
pageSource = await page.evaluate(selector => { | ||
return document.querySelector(selector).innerHTML; | ||
}, waitForSelector); | ||
} else { | ||
pageSource = await page.content(); | ||
} | ||
browser = await puppeteer.launch({ | ||
headless: options.puppeteerHeadless ?? true, | ||
args: ['--no-sandbox', '--disable-gpu', '--disable-setuid-sandbox'], | ||
timeout: options.puppeteerTimeout || 30_000, | ||
}); | ||
let page = await browser.newPage(); | ||
await page.setExtraHTTPHeaders(DEFAULT_HEADER); | ||
const response = await page.goto(url, { | ||
waitUntil: 'domcontentloaded', | ||
}); | ||
let pageSource; | ||
//if we're extracting data from a spa, we must wait for the selector | ||
if (waitForSelector != null) { | ||
await page.waitForSelector(waitForSelector); | ||
pageSource = await page.evaluate((selector) => { | ||
return document.querySelector(selector).innerHTML; | ||
}, waitForSelector); | ||
} else { | ||
pageSource = await page.content(); | ||
} | ||
|
||
const statusCode = response.status(); | ||
const statusCode = response.status(); | ||
|
||
if (botDetected(pageSource, statusCode)) { | ||
console.warn('We have been detected as a bot :-/ Tried url: => ', url); | ||
return null; | ||
} | ||
if (botDetected(pageSource, statusCode)) { | ||
console.warn('We have been detected as a bot :-/ Tried url: => ', url); | ||
return null; | ||
} | ||
|
||
return await page.content(); | ||
} catch (error) { | ||
console.error('Error executing with puppeteer executor', error); | ||
return null; | ||
} finally { | ||
if (browser != null) { | ||
await browser.close(); | ||
} | ||
return await page.content(); | ||
} catch (error) { | ||
console.error('Error executing with puppeteer executor', error); | ||
return null; | ||
} finally { | ||
if (browser != null) { | ||
await browser.close(); | ||
} | ||
} | ||
} | ||
} |