node.js - 使用 playwright 抓取本地 html 文件

标签 node.js playwright

我正在尝试抓取本地 HTML 文件以获得页面标题我正在使用剧作家 Node Js 库,但我无法将其拉出来,请给我帮助,这里是代码

import playwright from "playwright";


async function main() {
    const browser = await playwright.chromium.launch({
        headless: true// setting this to true will not run the UI
    });
    
    const page = await browser.newPage();
    await page.goto('C:\Users\weske\Desktop\WebScraping\index.html');
    let title = await page.title();
    console.log(title);
    await page.waitForTimeout(5000); // wait for 5 seconds
    await browser.close();
}

main();
// error messages
(node:15800) UnhandledPromiseRejectionWarning: page.goto: net::ERR_FILE_NOT_FOUND at 
c:UsersweskeDesktopWebScrapingindex.html
=========================== logs ===========================
navigating to "c:UsersweskeDesktopWebScrapingindex.html", waiting until "load"
============================================================
    at main (file:///C:/Users/weske/Desktop/WebScraping/node.js:11:13)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:15800) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `-- 
 unhandled-rejections=strict` (see 
 https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
    (node:15800) [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.

最佳答案

始终确保按照这种格式放置文件路径

`file://C:/Users/weske/Desktop/Webscraping/index.html`

关于node.js - 使用 playwright 抓取本地 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70009973/

相关文章:

python - Python Playwright 中的错误:playwright._impl._api_types.Error: net::ERR_ABORTED at ...等待直到 "load"错误 Python Playwright

python - 使用 Playwright for Python,如何从下拉列表中选择一个选项?

node.js - 是否可以在 URL 中不显示端口号的情况下使用 Node.js 服务器。

node.js - node_redis 更新已用内存信息

python - 如何用 Playwright Python 异步打开多个页面?

python - 如何使用Playwright与Metamask交互?

gitlab - Playwright 在 Gitlab 管道中失败,browserType.launch : Host system is missing dependencies

javascript - Node.js 中的循环 Promise

javascript - 在node.js中的elasticsearch中创建空索引

javascript - 以编程方式将文件上传到 ownCloud 服务器