Javascript Can';t使用木偶师从dolartoday.com上获取输入值
我想用以下命令刮除元素Javascript Can';t使用木偶师从dolartoday.com上获取输入值,javascript,node.js,web-scraping,puppeteer,Javascript,Node.js,Web Scraping,Puppeteer,我想用以下命令刮除元素的值#结果: const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://dolartoday.com'); await console.log(pag
的值
#结果
:
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.
(节点:74908)未处理的Promisejection警告:错误:超过导航超时:超过30000ms
在Promise.then(/Volumes/DATOS/Dropbox/workspace/dolartoday/server/node_modules/puppeter/lib/NavigatorWatcher.js:71:21)
在
(节点:74908)未处理的PromisejectionWarning:未处理的承诺拒绝。此错误源于在没有catch块的异步函数中抛出,或者拒绝未使用.catch()处理的承诺。(拒绝id:1)
(节点:74908)[DEP0018]弃用警告:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。
您知道如何解决这个问题吗?首先,您尝试在(同步函数)上使用运算符,而不是在(异步函数)上使用运算符 您还试图将页面DOM元素返回到Node.js环境中,该操作将不起作用,因为它需要返回值 如果要返回网页上
#result
元素的值
,则应按如下方式重写逻辑:
console.log(await page.evaluate(() => document.getElementById('result').value));
此外,导航时间已超过30000毫秒(默认最大值)。您可以使用函数中的timeout
选项扩展最大导航时间:
await page.goto('https://dolartoday.com', {
timeout: 60000,
});
您还可以使用和拒绝在网页中加载不必要的资源。这将使您的网页加载速度更快:
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();
})();
超过导航超时:超过30000ms