首页 .Net .NET(C#)中Puppeteer API的PuppeteerSharp的使用

.NET(C#)中Puppeteer API的PuppeteerSharp的使用

1、使用文档

PuppeteerSharp文档http://www.puppeteersharp.com/api/index.html

PuppeteerSharp源码https://github.com/kblok/puppeteer-sharp

2、安装PuppeteerSharp

使用Nuget搜索PuppeteerSharp,找到PuppeteerSharp点击安装即可。

相关文档:VS(Visual Studio)中Nuget的使用

3、PuppeteerSharp的使用

1)网页截图

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);
生成截图前也可以改变ViewPort:
await page.SetViewport(new ViewPortOptions
{
Width = 500,
Height = 500
});

2)网页保存成pdf文件

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);

3)向网页中注入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);
}

4)执行Javascript(js)代码

using (var page = await Browser.NewPageAsync())
{
var seven = await page.EvaluateFunctionAsync<int>("4 + 3");
var someObject = await page.EvaluateFunctionAsync<dynamic>("(value) => ({a: value})", 5);
Console.WriteLine(someObject.a);
}

5)等待Selector内容加载

using (var page = await Browser.NewPageAsync())
{
await page.GoToAsync("http://www.spapage.com");
await page.WaitForSelectorAsync("div.main-content")
await page.PdfAsync(outputFile));
}

6)等待满足Function条件

using (var page = await Browser.NewPageAsync())
{
await page.GoToAsync("http://www.spapage.com");
var watchDog = page.WaitForFunctionAsync("window.innerWidth < 100");
await Page.SetViewport(new ViewPortOptions { Width = 50, Height = 50 });
await watchDog;
}

7)连接到远程浏览器

var options = new ConnectOptions()
{
BrowserWSEndpoint = $"wss://www.externalbrowser.io?token={apikey}"
};
var url = "https://www.google.com/";
using (var browser = await PuppeteerSharp.Puppeteer.ConnectAsync(options))
{
using (var page = await browser.NewPageAsync())
{
await page.GoToAsync(url);
await page.PdfAsync("wot.pdf");
}
}



特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。