PuppeteerSharp讀取頁面完整HTML(.NetCore)

1.使用NUGET安裝PuppeteerSharp

經過工具或者命令方式安裝html

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();
        
        //針對源代碼進行分析
        var te = new TextExtractor(sourceFile);
        te.extract();
        sr.Close();
        
        //將頁面保存爲圖片
        //await page.ScreenshotAsync(@"D:\1.png",
        //    new ScreenshotOptions() { FullPage = true, Type = ScreenshotType.Png });
    }
}
相關文章
相關標籤/搜索