How to run Puppeteer code in any web browser?

前端 未结 1 1630
后悔当初
后悔当初 2020-11-29 10:47

I\'m trying to do some web scraping with Puppeteer and I need to retrieve the value into a Website I\'m building.

I have tried to load the Puppeteer file in the html

相关标签:
1条回答
  • 2020-11-29 11:03

    EDIT: Since puppeteer removed support for puppeteer-web, I moved it out of the repo and tried to patch it a bit.

    It does work with browser. The package is called puppeteer-web, specifically made for such cases.

    But the main point is, there must be some instance of chrome running on some server. Only then you can connect to it.

    You can use it later on in your web page to drive another browser instance through its WS Endpoint:

    <script src="https://unpkg.com/puppeteer-web">
    </script>
    
    <script>
      const browser = await puppeteer.connect({
        browserWSEndpoint: `ws://0.0.0.0:8080`, // <-- connect to a server running somewhere
        ignoreHTTPSErrors: true
      });
    
      const pagesCount = (await browser.pages()).length;
      const browserWSEndpoint = await browser.wsEndpoint();
      console.log({ browserWSEndpoint, pagesCount });
    </script>
    

    I had some fun with puppeteer and webpack,

    • playground-react-puppeteer
    • playground-electron-react-puppeteer-example

    See these answers for full understanding of creating the server and more,

    • Official link to puppeteer-web
    • Puppeteer with docker
    • Puppeteer with chrome extension
    • Puppeteer with local wsEndpoint
    0 讨论(0)
提交回复
热议问题