【发布时间】: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"&amp;amp;graphtype="xy"&amp;amp;profileid="19433213274429306"&amp;amp;element="72"&amp;amp;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。