如何使用 Puppeteer 打印 HTML 文档?

Ink*_*Kim 6 javascript web-crawler node.js google-chrome-devtools puppeteer

最近我开始使用 Puppeteer 爬行网络。下面是从商城中提取特定产品名称的代码。

\n\n
const puppeteer = require('puppeteer');\n\n(async () => {\n\n    const width = 1600, height = 1040;\n\n    const option = { headless: false, slowMo: true, args: [`--window-size=${width},${height}`] };\n\n    const browser = await puppeteer.launch(option);\n    const page = await browser.newPage();\n    const vp = {width: width, height: height};\n    await page.setViewport(vp);\n\n    const navigationPromise = page.waitForNavigation();\n\n    await page.goto('https://shopping.naver.com/home/p/index.nhn');\n    await navigationPromise;\n    await page.waitFor(2000);\n\n    const textBoxId = 'co_srh_input';\n    await page.type('.' + textBoxId, '\xec\x96\x91\xeb\xa7\x90', {delay: 100});\n    await page.keyboard.press('Enter');\n\n    await page.waitFor(5000);\n    await page.waitForSelector('div.info > a.tit');\n\n    const stores = await page.evaluate(() => {\n        const links = Array.from(document.querySelectorAll('div.info > a.tit'));\n        return links.map(link => link.innerText).slice(0, 10)   // 10\xea\xb0\x9c \xec\xa0\x9c\xed\x92\x88\xeb\xa7\x8c \xea\xb0\x80\xec\xa0\xb8\xec\x98\xa4\xea\xb8\xb0\n    });\n\n    console.log(stores);\n    await browser.close();\n\n})();\n
Run Code Online (Sandbox Code Playgroud)\n\n

我有个问题。如何将爬取的结果输出到HTML文档(不使用数据库)?请使用示例代码来解释它。

\n

小智 5

我使用了在blog.kowalczyk.info上看到的内容

const puppeteer = require("puppeteer");
const fs = require("fs");

async function run() {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
    await page.goto("https://www.google.com/", { waitUntil: "networkidle2" });
    // hacky defensive move but I don't know a better way:
    // wait a bit so that the browser finishes executing JavaScript
    await page.waitFor(1 * 1000);
    const html = await page.content();
    fs.writeFileSync("index.html", html);
    await browser.close();
}

run();
Run Code Online (Sandbox Code Playgroud)


Gra*_*ler 2

fs.writeFile()

\n

您可以使用以下函数,该函数在成功或失败时write_file返回解析或拒绝的值。Promisefs.writeFile()

\n

然后,您可以在匿名异步await函数Promise中检查数据是否已写入文件:

\n
\'use strict\';\n\nconst fs = require(\'fs\');\nconst puppeteer = require(\'puppeteer\');\n\nconst write_file = (file, data) => new Promise((resolve, reject) => {\n  fs.writeFile(file, data, \'utf8\', error => {\n    if (error) {\n      console.error(error);\n      reject(false);\n    } else {\n      resolve(true);\n    }\n  });\n});\n\n(async () => {\n  \n  // ...\n  \n  const stores = await page.evaluate(() => {\n    return Array.from(document.querySelectorAll(\'div.info > a.tit\'), link => link.innerText).slice(0, 10); // 10\xea\xb0\x9c \xec\xa0\x9c\xed\x92\x88\xeb\xa7\x8c \xea\xb0\x80\xec\xa0\xb8\xec\x98\xa4\xea\xb8\xb0\n  });\n  \n  if (await write_file(\'example.html\', stores.toString()) === false) {\n    console.error(\'Error: Unable to write stores to example.html.\');\n  }\n  \n  // ...\n  \n});\n
Run Code Online (Sandbox Code Playgroud)\n