【问题标题】:HtmlAgilityPack : illegal characters in pathHtmlAgilityPack :路径中的非法字符
【发布时间】:2014-03-22 13:34:55
【问题描述】:

我在此代码中收到“路径中的非法字符”错误。我在发生错误的行中提到了“此处发生错误”作为注释。

var document = htmlWeb.Load(searchUrl);
var hotels = document.DocumentNode.Descendants("div")
             .Where(x => x.Attributes.Contains("class") &&
             x.Attributes["class"].Value.Contains("listing-content"));

int count = 1;
foreach (var hotel in hotels)
{
    HtmlDocument htmlDoc = new HtmlDocument();
    htmlDoc.OptionFixNestedTags = true;
    htmlDoc.Load(hotel.InnerText);      // Error Occuring Here //
    if (htmlDoc.DocumentNode != null)
    {
        var hotelName = htmlDoc.DocumentNode.SelectNodes("//div[@class='business-container-inner']//div[@class='business-content clearfix']//div[@class='business-name-wrapper']//h3[@class='business-name fn org']//div[@class='srp-business-name']//a[0]");
        foreach (var name in hotelName)
        {
            Console.WriteLine(name.InnerHtml);
        }
    }
}

【问题讨论】:

    标签: c# html-parsing html-agility-pack


    【解决方案1】:

    您应该使用LoadHtml 方法加载一个字符串。 Load 方法从 文件

    加载
    htmlDoc.LoadHtml(hotel.InnerText);   
    

    【讨论】:

    • 谢谢你,LB。这行得通。我没有注意到那个小错误。感谢您的时间和回复。
    【解决方案2】:

    这仅仅意味着您正在尝试使用 invalid character in the file path/name 加载文件。

    错误在这里:

    htmlDoc.Load(hotel.InnerText); 
    

    ..因为重载需要文件的路径:

    public void Load(string path)
    

    使用LoadHtml 加载 HTML 片段:

    public void LoadHtml(string html)
    

    【讨论】:

      猜你喜欢
      • 2019-08-30
      • 1970-01-01
      • 2012-03-05
      • 2018-02-28
      • 2015-07-31
      • 2010-09-26
      • 1970-01-01
      相关资源
      最近更新 更多