0

例如:源代码将是:

<html>
<head> balaba....
</head>
<body>
<div id="many_div">...</div>


<div id="main">

     <div id="target">
     .....balabala ...
     </div>

</div>
</body></html>

那么,如何让我的浏览器只显示带有“target”id 的 div 呢?谢谢!

4

1 回答 1

0

您需要操作页面的 HTML。

我将使用HtmlAgilityPack提取您想要的部分并将其重写到相同或另一个文件:

Dim html = File.ReadAllText("c:\temp\htmlTest.htm")
Dim doc = New HtmlAgilityPack.HtmlDocument()
doc.LoadHtml(html)
Dim target = doc.GetElementbyId("target")
If target IsNot Nothing Then
    Dim body = doc.DocumentNode.SelectSingleNode("//body")
    body.RemoveAll()
    body.PrependChild(target)
    Using writer = File.OpenWrite("c:\temp\htmlTest2.htm")
        doc.Save(writer)
    End Using
End If

现在您只需将这个 html 加载到WebBrowser.

如果您想直接从 Internet/Intranet 获取 HTML:

Dim client As New HtmlAgilityPack.HtmlWeb()
Dim doc As HtmlAgilityPack.HtmlDocument = client.Load("http://yoururl.com")
' rest is the same as above(without doc.LoadHtml) '
于 2013-09-19T14:36:32.263 回答