gpt4 book ai didi

javascript - 无法使用 Puppeteer 从 dolartoday.com 抓取输入值

转载 作者:行者123 更新时间:2023-12-03 02:13:54 26 4
gpt4 key购买 nike

我想抓取元素#result:

 const puppeteer = require('puppeteer');

(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://dolartoday.com');
await console.log(page.evaluate(() => document.getElementById('result')));

await browser.close();
})();

但它仍然记录以下错误:

(node:74908) UnhandledPromiseRejectionWarning: Error: Navigation Timeout Exceeded: 30000ms exceeded
at Promise.then (/Volumes/DATOS/Dropbox/workspaces/dolar-today/server/node_modules/puppeteer/lib/NavigatorWatcher.js:71:21)
at <anonymous>
(node:74908) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:74908) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

知道如何解决这个问题吗?

最佳答案

首先,您正在尝试使用 await运算符 console.log() (同步函数),而不是 page.evaluate() (异步函数)。

您还尝试将 Page DOM 元素返回到 Node.js 环境,但这将不起作用,因为 page.evaluate()期待 serializable返回值。

如果您想返回网页上#result元素的value,您应该按如下方式重写您的逻辑:

console.log(await page.evaluate(() => document.getElementById('result').value));

此外,导航时间已超过 30000 毫秒(默认最大值)。您可以使用 page.goto() 中的 timeout 选项来扩展最大导航时间。功能:

await page.goto('https://dolartoday.com', {
timeout: 60000,
});

您还可以使用page.setRequestInterception()拒绝在网页中加载不必要的资源。和 page.on('request') 。这将使您的网页加载速度更快:

await page.setRequestInterception(true);

page.on('request', request => {
if (['image', 'stylesheet', 'font'].indexOf(request.resourceType()) !== -1) {
request.abort();
} else {
request.continue();
}
});

您的最终程序应如下所示:

'use strict';

const puppeteer = require('puppeteer');

(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();

await page.setRequestInterception(true);

page.on('request', request => {
if (['image', 'stylesheet', 'font'].indexOf(request.resourceType()) !== -1) {
request.abort();
} else {
request.continue();
}
});

await page.goto('https://dolartoday.com', {
timeout: 60000,
});

console.log(await page.evaluate(() => document.getElementById('result').value));

await browser.close();
})();

关于javascript - 无法使用 Puppeteer 从 dolartoday.com 抓取输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49442220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com