C#:用Playwright实现截长图

何甫
2023-12-01

很多时候,我们有网页链接,但希望拿到网页的长图,这个过程还是稍嫌麻烦的。

毕竟要拿出手机,手不停地滑动,直到最底下,比较累。

还好,现在有了Playwright。

Playwright 是微软官方的一个开源框架。具体说明还是贴原文吧:

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

官方的文档链接:

https://playwright.dev/dotnet/docs/intro

先贴个最直接的Demo (注:至少是VS2022+)

using Microsoft.Playwright;

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();
await page.GotoAsync("https://playwright.dev/dotnet");
await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });

简单几行代码,就能实现这么强大的功能,是不是很酷?

酷是酷,但代价也是挺大的,毕竟大伙用的不一定是VS2022,即使用了可能framework之类的也不是新的。

难道只能当玩具,看着流口水?

也不是,咱们曲线救国吧。

VS2022装还是要装的,不装很多东西没有,因为Playwright依赖的东西特别多。

与平时用的框架版本不一致怎么办?

Playwright写成一个单独的控制台程序,然后旧框架代码,进程调用这个控制台程序就可以了。

下面是自己写的一个控制台程序,抛砖引玉吧:

using Microsoft.Playwright;
using System.Threading.Tasks;

namespace PlaywrightDemo
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            try
            {
                bool noParas = (args == null || args.Length == 0);
                int firstWaitMs = noParas ? 5000 : Convert.ToInt32(args[0]);
                int secondWaitMs = noParas ? 10000 : Convert.ToInt32(args[1]);
                int viewWidth = noParas ? 500 : Convert.ToInt32(args[2]);
                int viewHeight = noParas ? 10000 : Convert.ToInt32(args[3]);
                string url = noParas ? "https://www.meipian.cn/2bgwp40x" : args[4];
                string outFilePath = noParas ? "d:\\1.png" : args[5];

                var playwright = await Playwright.CreateAsync();
                IBrowser browser = await playwright.Chromium.LaunchAsync();
                var page = await browser.NewPageAsync();
                await page.GotoAsync(url);
                Console.WriteLine("Begin:{0}",DateTime.Now);
                await page.WaitForTimeoutAsync(firstWaitMs);
                await page.SetViewportSizeAsync(viewWidth, viewHeight);
                await page.Mouse.WheelAsync(0, viewHeight);
                await page.WaitForTimeoutAsync(secondWaitMs);
                await page.ScreenshotAsync(new PageScreenshotOptions
                {
                    Path = "screenshot.png",
                    FullPage = true
                });
                if (File.Exists(outFilePath))
                {
                    File.Delete(outFilePath);
                }
                File.Copy("screenshot.png", outFilePath);
                Console.WriteLine("End:{0}", DateTime.Now);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

写到这里呢,自己也不是太满意,毕竟要如此的劳神费事,还是太笨拙。

如哪位朋友有更好的办法,欢迎拍砖。

Hello Playwright:(1)从开发到部署

Hello Playwright:(2)简化部署方式

 类似资料: