30

PuppeteerSharp: 更友好的 Headless Chrome C# API

 5 years ago
source link: http://www.cnblogs.com/shanyou/p/10500049.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

前端就有了对 headless 浏览器的需求,最多的应用场景有两个

  1. UI 自动化测试:摆脱手工浏览点击页面确认功能模式
  2. 爬虫:解决页面内容异步加载等问题

也就有了很多杰出的实现,前端经常使用的莫过于 PhantomJSselenium-webdriver ,但两个库有一个共性——难用!环境安装复杂,API 调用不友好,1027 年 Chrome 团队连续放了两个大招 Headless Chrome 和对应的 NodeJS API Puppeteer ,直接让 PhantomJS 和 Selenium IDE for Firefox 作者宣布没必要继续维护其产品.

Puppeteer

如同其 github 项目介绍:Puppeteer 是一个通过 DevTools Protocol 控制 headless chrome 的 high-level Node 库,也可以通过设置使用 非 headless Chrome

我们手工可以在浏览器上做的事情 Puppeteer 都能胜任

  1. 生成网页截图或者 PDF
  2. 爬取大量异步渲染内容的网页,基本就是人肉爬虫
  3. 模拟键盘输入、表单自动提交、UI 自动化测试

PuppeteerSharp

Puppeteer Sharp是官方 Node.JS Puppeteer API 的.NET 移植

  1. 由于Puppeteer-Sharp是NetStandard 2.0库,因此最低平台版本是.NET Framework 4.6.1和.NET Core 2.0
  2. 支持WebSocket库的最低Windows版本是Windows 8和Windows Server 2012,具体参见 https://docs.microsoft.com/en-us/dotnet/api/system.net.websockets?redirectedfrom=MSDN&view=netframework-4.6.1

这个项目很活跃,大家可以通过作者博客上获取更新记录 http://www.hardkoded.com/

博客园里有一篇文章 PuppeteerSharp+AngleSharp的爬虫实战之汽车之家数据抓取https://www.cnblogs.com/VAllen/p/PuppeteerSharp-AngleSharp-CrawlerSamples.html 更详细的介绍了如何使用PuppeterSharp。在github上还有一个PuppeterSharp的扩展项目: https://github.com/hlaueriksson/puppeteer-sharp-contrib


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK