vb.net 使用 htmlagilitypack 在 iframe 中获取 src 链接

vb.net get src links insade iframes with htmlagilitypack

我正在使用 html 敏捷并尝试同时获得 wanted1wanted2 html 代码是这样的

<div class='class1' id='id1'>
<iframe id="iframe1" src="wanted1"</iframe>
<iframe id="iframe" src="wanted2"</iframe>
</div>

但是运气不好有人可以帮助我吗

这是一个带注释的示例,可帮助您入门:

        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。