VB.NET Webbrowser如何显示我想要的指定内容

时间:2013-09-19 14:29:38

标签: vb.net

例如:源代码将是:

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


<div id="main">

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

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

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

1 个答案:

答案 0 :(得分: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

现在你只需要在WebBrowser

中加载这个html

如果您想直接从互联网/内联网获取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) '