将PuppeteerSharp与.net核心API结合使用

时间:2019-05-20 14:25:11

标签: asp.net-core puppeteer-sharp

我正在跟踪样本“ PupppeterSharpAspNetFrameworkSample”(4.7.2)。但是我不能使它工作。是否可以提供一些代码或链接,说明该单词在.net core api 2.2中的工作原理?

public class BrowserClient
{
    private static readonly string HostPath = HostingEnvironment.MapPath("~/App_Data/");

    public static async Task<string> GetTextAsync(string url)
    {
        var browserFetcher = new BrowserFetcher(new BrowserFetcherOptions()
        {
            Path = HostPath
        });
        await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision).ConfigureAwait(false);

        using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions()
        {
            Headless = true,
            ExecutablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision)
        }).ConfigureAwait(false))
        using(var page = await browser.NewPageAsync().ConfigureAwait(false))
        {
            var response = await page.GoToAsync(url).ConfigureAwait(false);
            return await response.TextAsync().ConfigureAwait(false);
        }
    }
}

}

它引发的错误是

{"Message":"An error has occurred.","ExceptionMessage":"Failed to create connection","ExceptionType":"PuppeteerSharp.ChromiumProcessException","StackTrace":"   at PuppeteerSharp.Launcher.<LaunchAsync>d__7.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at PuppeteerSharp.Launcher.<LaunchAsync>d__7.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n   at PupppeterSharpAspNetFrameworkSample.Services.BrowserClient.<GetTextAsync>d__1.MoveNext() in D:\\development\\screenscraping\\puppeteer-sharp\\samples\\PupppeterSharpAspNetFrameworkSample\\PupppeterSharpAspNetFrameworkSample\\Services\\BrowserClient.cs:line 20\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__1`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Protocol error(Target.setDiscoverTargets): Target closed. (An internal WebSocket error occurred. Please see the innerException, if present, for more details. )","ExceptionType":"PuppeteerSharp.TargetClosedException","StackTrace":"   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n   at PuppeteerSharp.Connection.<SendAsync>d__34.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n   at PuppeteerSharp.Browser.<CreateAsync>d__73.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n   at PuppeteerSharp.Launcher.<LaunchAsync>d__7.MoveNext()"}}

0 个答案:

没有答案