1.使用NUGET安裝PuppeteerSharp 通過工具或者命令方式安裝 2.初始化瀏覽器 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision); 3.具體代碼 using (Browser browse ...
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 }); } }