【问题标题】:HTML Agility Pack parsing long url with params in src attributes for imagesHTML Agility Pack 使用图像的 src 属性中的参数解析长 url
【发布时间】:2011-05-14 08:23:14
【问题描述】:

在使用 HAP 解析 html 文档时,我遇到了图像的 src 属性问题。 id src 属性值是一个带参数的长url,例如:<img border='0' title='Kommunelogo' alt='Kommunelogo' style='margin-top: 5px;' src='http://livskraftig.bedrekommune.no/more/reports/profilechart.jsp?legend=Y&graphtype=xy&profileid=19433213274429306&element=72&addyears=true' />

然后 HAP 会像这样解析图像:<img border='0' title='Kommunelogo' alt='Kommunelogo' style='margin-top: 5px;' src='http://livskraftig.bedrekommune.no/more/reports/profilechart.jsp?legend="Y"&graphtype="xy"&profileid="19433213274429306"&element="72"&addyears="tru"e'/>

看起来 HAP 将参数拆分为属性。

我的代码:

HtmlDocument doc = new HtmlDocument();
doc.OptionOutputAsXml = true;
doc.OptionAutoCloseOnEnd = true;
doc.OptionFixNestedTags = true;
doc.LoadHtml(input_which_is_a_whole_html_file);

HtmlAgilityPack.HtmlNodeCollection imageNodes = doc.DocumentNode.SelectNodes("//img");
if (imageNodes != null)
{
    foreach (HtmlAgilityPack.HtmlNode imgNode in imageNodes)
    {
        string imgSrc = imgNode.Attributes["src"].Value;
    }
}

有什么办法可以避免这种情况吗?

非常感谢!

【问题讨论】:

  • 您能否向我们提供您要解析的整个文档?在仅使用该“img”的文档中测试您的代码,HAP 会完美地返回 url。

标签: html html-agility-pack


【解决方案1】:

您的代码可能在做一些奇怪的事情,因为以下工作正常:

    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml("<img border='0' title='Kommunelogo' alt='Kommunelogo' style='margin-top: 5px;' src='http://livskraftig.bedrekommune.no/more/reports/profilechart.jsp?legend=Y&graphtype=xy&profileid=19433213274429306&element=72&addyears=true' />");
    doc.Save(Console.Out);

你有复制品吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2014-07-13
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多