2010-11-23 83 views
1

当使用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' />HTML敏捷包使用src属性解析图片的长链接

那么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拆分PARAMS以为他们都是属性。

我的代码:

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; 
    } 
} 

任何想法如何避免这种情况?

非常感谢!

+1

您能否向我们提供您想要解析的整个文档?在仅使用'img'的文档中测试代码,HAP可以完美地返回网址。 – 2010-12-04 08:49:03

回答

0

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

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); 

你有一个摄制?

+0

编辑并添加了一些代码。谢谢! – 2010-11-23 10:35:30