【问题标题】:How can I do web scraping via puppeteer如何通过 puppeteer 进行网页抓取
【发布时间】:2021-02-06 22:17:36
【问题描述】:

如果我转到https://investor.vanguard.com/mutual-funds/profile/VMMXX 并从控制台执行document.querySelector("[data-ng-if='productSummaryTitle']").innerText,我会得到我所期望的:Product summary

但是当我尝试对puppeteer 做同样的事情时,我得到了UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Cannot read property 'innerText' of null at __puppeteer_evaluation_script__:3:83。我错过了什么?

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch({ headless: false })
    const page = await browser.newPage()
    await page.goto('https://investor.vanguard.com/mutual-funds/profile/VMMXX')

    const result = await page.evaluate(() => {
        let myText = document.querySelector("[data-ng-if='productSummaryTitle']").innerText
        return {
            myText
        }
    })

    console.log(result)

    browser.close()
})()

【问题讨论】:

    标签: javascript node.js puppeteer


    【解决方案1】:

    你可以先等待那个选择器

    const element = await page.waitForSelector('[data-ng-if='productSummaryTitle']');
    const text = await element.evaluate(el => el.innerText);
    

    【讨论】:

      【解决方案2】:
      const puppeteer = require('puppeteer');
      
      (async () => {
      const browser = await puppeteer.launch();
      const page = await browser.newPage();
      await page.goto('https://investor.vanguard.com/mutual-funds/profile/VMMXX');
      const element = await page.waitForSelector("[data-ng-if='productSummaryTitle']");
      const text = await element.evaluate(el => el.innerText);
      console.log(text);
      await browser.close();
      })();
      

      【讨论】:

      • 感谢您提供答案。您能否编辑您的答案以包括对您的代码的解释?这将有助于未来的读者更好地了解正在发生的事情,尤其是那些刚接触该语言并难以理解概念的社区成员。
      猜你喜欢
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      相关资源
      最近更新 更多