【问题标题】:How to scrape a react Website using Puppeteer?如何使用 Puppeteer 抓取反应网站?
【发布时间】:2020-03-15 14:34:34
【问题描述】:

我正在尝试抓取一个使用 react 的网站,在 chrome 中我可以访问我想要的元素,但是当我尝试使用 puppeteer 时,我无法访问与源代码未加载相同的元素那些反应元素,有什么办法可以访问它。

这里是代码

const request= require('request-promise')
const cheerio= require('cheerio');
const puppeteer = require("puppeteer");

const Url='https://angel.co/companies';


(async ()=>{

    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();

    await page.goto(Url);
      const html = await page.content();
    // const response=request.get(Url)
    const $ = await cheerio.load(html);




    // const companies=$('div.base.startup').html()
     const companies=await $('div.base.startup > div.company.column > div > div.text > div.pitch').text()
    // const companies=document.querySelectorAll('div.base.startup > div.company.column > div > div.text > div.pitch')

    console.log(companies)
    await browser.close()

} )()

【问题讨论】:

  • 你得到的输出是什么?既然你说它是一个react网站,那可能是渲染没有发生。
  • 我没有得到任何输出,它显示为空白

标签: javascript node.js reactjs puppeteer


【解决方案1】:

几个月前我用 Python 做了一些类似的事情。 基本上是在等待一个元素出现在页面上(作为指示,表示 React 完成加载)。

我查找了 Puppeteer 的 API 并找到了 page.waitForSelector,我猜你可以使用它。

【讨论】:

  • 谢谢,正是我需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-21
  • 1970-01-01
  • 1970-01-01
  • 2023-01-09
  • 2020-10-07
  • 1970-01-01
  • 2020-06-20
相关资源
最近更新 更多