1.使用NUGET安装PuppeteerSharp
通过工具或者命令方式安装
2.初始化浏览器
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
3.具体代码
using (Browser browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true })) { using (var page = await browser.NewPageAsync()) { //设置浏览器的页面大小 await page.SetViewportAsync(new ViewPortOptions { Width = 1024, Height = 768 }); await page.GoToAsync("http://www.baidu.com"); var html = await page.GetContentAsync(); var sourceFile = ""; var memoryStream = new MemoryStream(Encoding.Default.GetBytes(html)); var sr = new StreamReader(memoryStream); sourceFile = sr.ReadToEnd(); //针对源代码进行分析 sr.Close(); //将页面保存为图片 //await page.ScreenshotAsync(@"D:1.png", // new ScreenshotOptions() { FullPage = true, Type = ScreenshotType.Png }); } }