首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用playwright抓取本地html文件

用playwright抓取本地html文件
EN

Stack Overflow用户
提问于 2021-11-17 18:48:50
回答 1查看 196关注 0票数 1

我正在尝试抓取一个本地HTML文件,以便获得页面标题。我使用的是playwright节点Js库,但我无法将其通过,请给我帮助这里是代码

代码语言:javascript
运行
复制
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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-17 19:04:08

请始终确保将文件路径放入如下格式

代码语言:javascript
运行
复制
`file://C:/Users/weske/Desktop/Webscraping/index.html`
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70009973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档