PuppeteerSharp: 更友好的 Headless Chrome C# API

前端就有了對 headless 瀏覽器的需求,最多的應用場景有兩個javascript

  1. UI 自動化測試:擺脫手工瀏覽點擊頁面確認功能模式
  2. 爬蟲:解決頁面內容異步加載等問題

也就有了不少傑出的實現,前端常用的莫過於 PhantomJSselenium-webdriver,但兩個庫有一個共性——難用!環境安裝複雜,API 調用不友好,1027 年 Chrome 團隊連續放了兩個大招 Headless Chrome 和對應的 NodeJS API Puppeteer,直接讓 PhantomJS 和 Selenium IDE for Firefox 做者宣佈不必繼續維護其產品.html

Puppeteer

如同其 github 項目介紹:Puppeteer 是一個經過 DevTools Protocol 控制 headless chrome 的 high-level Node 庫,也能夠經過設置使用 非 headless Chrome前端

咱們手工能夠在瀏覽器上作的事情 Puppeteer 都能勝任java

  1. 生成網頁截圖或者 PDF
  2. 爬取大量異步渲染內容的網頁,基本就是人肉爬蟲
  3. 模擬鍵盤輸入、表單自動提交、UI 自動化測試

PuppeteerSharpgit

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

  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/web

博客園裏有一篇文章 PuppeteerSharp+AngleSharp的爬蟲實戰之汽車之家數據抓取https://www.cnblogs.com/VAllen/p/PuppeteerSharp-AngleSharp-CrawlerSamples.html 更詳細的介紹瞭如何使用PuppeterSharp。在github上還有一個PuppeterSharp的擴展項目:https://github.com/hlaueriksson/puppeteer-sharp-contrib chrome

相關文章:docker

相關文章
相關標籤/搜索