【问题标题】:vb.net get src links insade iframes with htmlagilitypackvb.net 使用 htmlagilitypack 获取 src 链接插入 iframe
【发布时间】:2016-03-01 08:56:34
【问题描述】:
我正在使用 htmlagility 并尝试同时获取 wanted1 和 wanted2
html代码是这样的
<div class='class1' id='id1'>
<iframe id="iframe1" src="wanted1"</iframe>
<iframe id="iframe" src="wanted2"</iframe>
</div>
但是运气不好,有人可以帮我吗
【问题讨论】:
标签:
vb.net
iframe
html-agility-pack
src
【解决方案1】:
这是一个带注释的示例,可帮助您入门:
Dim htmlDoc As New HtmlAgilityPack.HtmlDocument
Dim html As String = <![CDATA[<div class='class1' id='id1'>
<iframe id="iframe1" src="wanted1"</iframe>
<iframe id="iframe" src="wanted2"</iframe>
</div>]]>.Value
'load the html string to the HtmlDocument we defined
htmlDoc.LoadHtml(html)
'using LINQ and some xpath you can target any node you want
' //iframe[@src] xpath passed to the SelectNodes function means select all iframe nodes that has src attribute
Dim srcs = From iframeNode In htmlDoc.DocumentNode.SelectNodes("//iframe[@src]")
Select iframeNode.Attributes("src").Value
'print all the src you got
For Each src In srcs
Console.WriteLine(src)
Next
确保您了解 XPath。