关于 Puppeteer Sharp 的介绍
2020-09-09 22:20 音乐让我说 阅读(1026) 评论(0) 编辑 收藏 举报前言
项目地址:https://github.com/hardkoded/puppeteer-sharp
Puppeteer Sharp is a .NET port of the official Node.JS Puppeteer API.
As Puppeteer-Sharp is a NetStandard 2.0 library, the minimum platform versions are .NET Framework 4.6.1 and .NET Core 2.0. Read more.
The minimum Windows versions supporting the WebSocket library are Windows 8 and Windows Server 2012. Read more.
If you need to run Puppeteer-Sharp on Windows 7 you can use System.Net.WebSockets.Client.Managed through the LaunchOptions.WebSocketFactory property.
If you have issues running Chrome on Linux, the Puppeteer repo has a great troubleshooting guide.
X-server is required on Linux.
使用说明
Take screenshots
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision); var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }); var page = await browser.NewPageAsync(); await page.GoToAsync("http://www.google.com"); await page.ScreenshotAsync(outputFile);
You can also change the view port before generating the screenshot
await page.SetViewportAsync(new ViewPortOptions { Width = 500, Height = 500 });
Generate PDF files
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision); var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }); var page = await browser.NewPageAsync(); await page.GoToAsync("http://www.google.com"); await page.PdfAsync(outputFile);
Inject HTML
using(var page = await browser.NewPageAsync()) { await page.SetContentAsync("<div>My Receipt</div>"); var result = await page.GetContentAsync(); await page.PdfAsync(outputFile); SaveHtmlToDB(result); }
Evaluate Javascript
using (var page = await browser.NewPageAsync()) { var seven = await page.EvaluateExpressionAsync<int>("4 + 3"); var someObject = await page.EvaluateFunctionAsync<dynamic>("(value) => ({a: value})", 5); Console.WriteLine(someObject.a); }
Wait For Selector
using (var page = await browser.NewPageAsync()) { await page.GoToAsync("http://www.spapage.com"); await page.WaitForSelectorAsync("div.main-content") await page.PdfAsync(outputFile)); }
谢谢浏览!
作者:音乐让我说(音乐让我说 - 博客园)
出处:http://music.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步