HtmlAgilityPack HtmlWeb.Load returning empty Document

前端 未结 1 1502
Happy的楠姐
Happy的楠姐 2020-12-07 00:09

I have been using HtmlAgilityPack for the last 2 months in a Web Crawler Application with no issues loading a webpage.

Now when I try to load a this particular webpa

1条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 00:45

    It seems this website requires cookies to be enabled. So creating a cookie container for your web request should solve the issue:

    var url = "http://www.prettygreen.com/";
    var htmlWeb = new HtmlWeb();
    htmlWeb.PreRequest += request =>
        {
            request.CookieContainer = new System.Net.CookieContainer();
            return true;
        };
    var htmlDoc = htmlWeb.Load(url);
    var outerHtml = htmlDoc.DocumentNode.OuterHtml;
    Assert.AreNotEqual("", outerHtml);
    

    0 讨论(0)
提交回复
热议问题