使用HtmlAgilityPack获取i帧源
我想在html doc上获取所有iFrame源URL。 我尝试使用带有xpath的HtmlAgilityPack – 但我似乎没有得到一个源列表。
HtmlAgilityPack.HtmlDocument myHtml= new HtmlDocument(); myHtml.LoadHtml(htmlString); foreach (HtmlNode framesrc) in myHtml.DocumentNode.SelectNodes("//iframe/src")) { srcCollection.add(framesrc); }
我的xpath错了吗? 谢谢!
实际上这个开源html解析器使用查询看起来像下面的查询:
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//iframe[@src]"); foreach(var node in nodes){ HtmlAttribute attr = node.Attributes["src"]; Console.WriteLine(attr.Value); }
ifarme
具有@src
属性。 所以你的XPath应该是//iframe/@src
。 它将选择所有iframe
@src
。