提问人:daniel gi 提问时间:7/18/2023 最后编辑:daniel gi 更新时间:7/20/2023 访问量:70
Puppeteer 始终在远程服务器上加载移动脚本
Puppeteer always loads mobile script on remote server
问:
我正在尝试抓取(无头)此 URL 的脚本,但我注意到每当我在本地机器上执行此操作时,我都会得到:脚本。"https://vidstat.taboola.com/lite-unit/4.1.0/UnitFeedManagerDesktop.min.js"
问题是当我调用API在远程服务器(邮递员)上抓取时,我总是得到一个脚本,该脚本应该只出现在移动设备上:https://vidstat.taboola.com/lite-unit/4.1.0/UnitFeedManagerMobile.min.js"
这是我的代码:
public async fetchScripts(url: string, waitFor = 'cdn-pipes.js') {
const page = await this.browser.newPage();
try {
await page.goto(url, {timeout: 10000, waitUntil: 'domcontentloaded'});
const func = waitFor ? `document.documentElement.innerHTML.indexOf("${waitFor}") !== -1 || document.documentElement.innerHTML.indexOf("spa-detector") !== -1` :
'document.readyState === "complete"';
await page.waitForFunction(func, {polling: 500, timeout: 8000}).catch(reason => {
console.error('page.waitForFunction', {error: reason, url});
});
const pageUrls = await page.evaluate(() => {
const urlArray = Array.from(document.scripts).map((link) => link.src).filter(value => value.includes('taboola.com'));
return [...new Set(urlArray)];
});
console.log('fetchMinimal - urlsArray ', {pageUrls});
return pageUrls;
} catch (e) {
console.error('fetchMinimal - error ', e);
} finally {
await page.close();
}
}
我怀疑这是一个 CDN 问题,以某种方式保存旧脚本 IDK,有什么想法吗?
更新:
发生这种情况是因为该页面仅在 chromium-browser 上始终为 true 时才会加载移动脚本。window.matchMedia(" only screen and (min-device-width : 320px) and (max-device-width : 480px)").matches
答:
0赞
daniel gi
7/20/2023
#1
多亏了这个答案 - 我设法通过提供args: ['--window-size=1920,1080']
puppeteer.launch
评论