网络抓取:Puppeteer 与 Selenium

Web scraping is the process of extracting data from websites. You can scrape manually using web browsers or automate it with libraries like Selenium and Puppeteer.

Selenium 是一款开源工具,可让您自动运行网络应用程序并测试网站功能,而无需在浏览器中编写任何代码。它还可与 Java、Python、C# 等其他编程语言配合使用。

Puppeteer 是一个 JavaScript 库,用于在无头 Linux 系统上自动测试 Chrome 浏览器。Puppeteer 支持 Chrome 浏览器,已在 Ubuntu 16.04 LTS、18.04 LTS 和 Debian 9 Stretch/Buster/Siduction(64 位)上进行了测试。 

在本文中,我们将学习如何在 NodeJS 中使用 Selenium WebDriver 和 Puppeteer,以便使用 Chrome 浏览器自动运行应用程序。

菜单

什么是 Puppeteer?

Puppeteer 是一个 JavaScript 库,可让您使用 puppeteer.js 控制浏览器。该库提供用于创建、管理和与网页交互的 API,无需依赖任何第三方。它还支持捕捉鼠标事件和键盘输入,以便与页面进行交互。

Puppeteer 如何工作?

Puppeteer 是一个用于控制 Chrome 浏览器的节点库。它允许你使用 JavaScript 控制浏览器,并以仅使用 HTML、CSS 和 JS 无法实现的方式与浏览器交互。

您可以用它来模拟鼠标/键盘事件、截图、操作 DOM 元素、发送 HTTP 请求等。Puppeteer 还有一个 API,允许你编写 puppeteering 脚本,而无需直接使用它的任何 API。有关 API 的更多信息,请访问:https://github.com/puppeteerjs/puppeteer-api。

Puppeteer 解决了以下问题:如何在无法运行 JavaScript 的情况下控制浏览器?Puppeteer 可让你模拟鼠标/键盘事件、截图、操作 DOM 元素、发送 HTTP 请求等。您还可以编写 puppeteering 脚本,而无需直接使用其任何 API。如果你想创建自己的 Chrome 浏览器扩展,与浏览器进行意外交互,这将非常有用。

复杂性和自动化环境与日俱增,因此一种工具可能无法解决所有问题。Puppeteer 有一些局限性。它只支持 Chrome 浏览器。适用于 Firefox 的 Puppeteer 正在开发中。

使用 Puppeteer 有哪些优势? 

Puppeteer 提供了一种在浏览器中运行脚本的简单方法,而无需自己编写脚本。它简单、轻便、可扩展。你可以将它与任何编程语言或框架(NodeJS、Python、Java)一起使用。它有自己的应用程序接口(API),因此您不必为了网络刮擦而学习其他应用程序接口。

使用 Puppeteer 不需要了解 Selenium 的工作原理,但仍能获得其所有优势:自动化、可测试性和可移植性!

使用 Puppeteer 有什么缺点?

Puppeteer 是一个非常年轻的框架。它仍处于测试阶段,不支持所有浏览器(只支持 Chrome、Firefox、Opera 和 Safari)。不过,随着时间的推移,Puppeteer 将能支持更多浏览器。API 也不如 Selenium 成熟,所以最初可能会遇到一些问题,但随着时间的推移会越来越好。你可以在 Puppeteer 的官方网站上找到许多如何使用 Puppeteer 的示例:puppeteerjs.com/docs/getting-started/。 

如何安装 Puppeteer? 

你可以从网站下载软件包或手动安装 Puppeteer。访问 Puppeteer 存储库页面,为你的操作系统下载最新版本。要运行它,需要在计算机上安装 NodeJS。如果不知道从哪里开始,请访问此处:nodejs.org/en/download。运行 npm install -g puppet。这将下载运行 puppetserver 所需的所有依赖项。现在在你的主目录下打开一个终端窗口,输入 puppetserver。 

如果一切顺利,您应该会看到这样的内容: 

$ node server Starting server… done! http://localhost:4200 Connecting…done! http://localhost:4200 Disconnected / Connected http://localhost:4200 <– Start scraping now! 

要停止,请按 Ctrl+C,然后键入退出。 

什么是硒?

Selenium is a potent tool that you can use to automate web-based applications. It is also commonly referred to as the Webdriver. Selenium has been around for quite some time, and it has become one of the most popular tools in the IT industry. 

硒有什么作用?

Selenium allows you to automate your websites by executing different actions such as clicking buttons, filling out forms, or even navigating through pages. The main goal of this software is to make automated testing easy and efficient without having to write any code yourself. You can use this software from your browser (Chrome/Firefox) or within a programming language like Java, C# or Python, etc. Once installed on your computer, you must download the selenium server, which runs on port 4444 (default). Then open up Chrome/Firefox and type in chrome://extensions into the address bar where you will see a list of available extensions; select Manage Extensions, then click the Load Unpacked Extension button.

您可以使用 Selenium 测试网站、移动应用程序和桌面应用程序。它简单易用,让你无需任何编程知识就能自动化基于网络的应用程序。您还可以使用 Selenium IDE 创建自己的测试,它有一个可视化界面,可以以用户友好的方式开发测试。

硒的优势

  • 它适用于所有网络浏览器(Chrome、Firefox、IE)和移动设备(安卓)。Selenium 是跨平台的。您只需为您选择的平台安装一个集成开发环境,然后开始使用 API。您不必为每个平台学习新的 API 或不同的语言,只需使用一种可在所有平台上运行的语言即可。 
  • Selenium 支持大多数编程语言,包括 Java、C#、Python 等。如果你想用 JavaScript 自动执行某些操作,可以使用 NodeJS 和 selenium-nodejs。 
  • Selenium 支持 Protractor、WebDriver 等多个框架。这些框架的最大优点是提供了一个很好的抽象层,比直接在浏览器中编写纯粹的浏览器自动化代码更简单。例如,如果我们想让测试同时在多个浏览器上运行,就可以使用 selenium-web driver 和 selenium-webdriver2 来实现。这意味着我们在同时跨多个浏览器进行测试时不会遇到任何问题,因为一切都将由框架本身处理,而不是在每个浏览器中手动编写自定义代码(这将使我们的测试套件看起来更加复杂)。 
  • Selenium 可使用许多插件,如 Google Chrome 浏览器扩展、火狐浏览器扩展等。还有什么?大量实例!不仅有开源项目,还有商业项目,如 Appium、Sauce Labs 或 Watir。因此,如果你正在寻找简单快捷的工具,不妨试试它们!它们都是很棒的工具!它们还支持异步测试,这意味着即使您的测试耗时可能超过预期,也不会影响其他并行运行的测试,不会阻塞彼此的执行流程(异步模式)。这样,我们就可以编写快速的用户界面测试,而不必担心拖慢应用程序的运行速度。 

Selenium 和 Puppeteer 的市场趋势表明,在过去几年中,特别是自 2014 年 Selenium 2 发布以来,其使用率显著上升。此后,Selenium 的受欢迎程度继续进一步增长,这意味着在可预见的未来,它很可能会继续以这种速度增长。 

网络测试的未来是什么? 

开源社区不断开发新技术并改进现有技术,丝毫没有放慢脚步的迹象。只要我们继续创新和创造新工具,我们就能期待更多网络测试技术的改进。

网络自动化工具:为什么要使用它们? 

当您的团队成员需要在他们的项目中工作时,或者当他们需要帮助用自动化测试来测试他们的代码库时,如果能使用 WebDriver 这样的自动化工具,就会比他们每次手动测试时手动尝试解决方案要容易得多。 

有了 WebDriver 这样的网络自动化工具,您就可以用它来自动化测试并用不同的语言编写测试。您还可以使用它与浏览器进行交互,测试应用程序中难以或无法手动完成的特定功能。可能性是无限的!

使用自动化工具可以让您为代码库编写自动化测试,而不必担心代码库的运行情况,从而让您的工作变得更加轻松。 

Selenium 还是 Puppeteer?哪种方法最适合网络抓取?

两者的主要区别在于,Puppeteer 在 Node.js 中运行,而 Selenium 在浏览器中运行。

You can use Puppeteer for web scraping and API testing, but it’s not as widely supported as Selenium. It also doesn’t have a built-in UI test runner, so you’ll need to write your own if you want to use it with a headless browser like PhantomJS or NightwatchJS.

与 Puppeteer 相比,Selenium 获得了更多浏览器的支持,其 UI 测试运行程序可在大多数流行浏览器(包括 Chrome 浏览器)上开箱即用。不过,与 Puppeteer 相比,Selenium 仍缺少一些功能。值得注意的是,WebDriver API 用于与页面上的元素进行交互,这意味着仅使用该库很难跨多个页面/域/应用程序自动执行复杂的交互(如点击链接)。 

Now is the right moment to mention that our rotating residential proxies vastly improve web scraping operations with both Selenium and Puppeteer.

In this Article:
Leave behind the complexities of web scraping.
Opt for IPBurger’s advanced web intelligence solutions to effortlessly collect real-time public data.
注册

更深入地了解

未分类
AJ Tait
The Best Storm Proxies Alternative: Faster, Safer & More Affordable Proxies

Looking for a Storm Proxies Alternative? Storm Proxies sells the dream: simple, affordable proxies that “just work.” And for some users? It kind of delivers. Until it doesn’t. Because here’s the reality—if you’re pulling small data sets, running light scraping jobs, or dipping your toes into sneaker copping, Storm Proxies

代理
AJ Tait
The Best IPRoyal Alternative for Power, Control & Price

Outgrowing IPRoyal? Here’s the Smarter Alternative If you’ve been using IPRoyal, chances are it was for one simple reason—it’s cheap. And in the world of proxies, “cheap” can be enough to get you started. But here’s the thing about IPRoyal: it’s a gateway, not a long-term solution. At first, it

Scale Your Business
With The Most Advanced
Proxies On Earth
加入屡获殊荣的第一代理网络