VB.NET-如何在网页源中搜索特定的div并在txt中输出为msgbox



VB。NET-我如何在网页源中搜索特定的div并在txt中输出为msgbox?

到目前为止,我可以下载一个网页的源代码。但我不知道如何在其中搜索特定的信息串。

到目前为止的代码:

Dim source As String = New 
System.Net.WebClient().DownloadString("http://pagewantingtouse.com")

这个部分被称为"描述",我在寻找其中的信息。我想把它作为一个消息框输出。

以下示例:

<div class="description">       
 The Amazing Spider-Man is a 2012 American superhero film based on the Marvel
 Comics character Spider-Man. It is the fourth installment of the Spider-Man film series, serving
 as a reboot.
</div>

我在一个项目中就有这个。它获取页面并将其流式读取到网络浏览器中,然后从中提取出你想要的任何内容。我以前看过读取字符串,至少对我来说,问题是你必须找到标签之间的长度,等等。这对我来说很好:

 Dim request As WebRequest = WebRequest.Create("http://pagewantingtouse.com") 'create a web request to the html file
    Using response As WebResponse = request.GetResponse() 'get the response back from the request
        Using Reader As New StreamReader(response.GetResponseStream) 'identify how you want to read the response and assign it to streamreader
            Dim webtext As String = Reader.ReadToEnd() 'read the response (web page) as a string to the end of the file
 Dim wbc As WebBrowser = New WebBrowser() 'create a web browser to handle the data. this will help sift through it
            wbc.DocumentText = ""
            With wbc.Document
                .OpenNew(True)
                .Write(webtext) 'write the web page response into the web browser control

                Dim itemlist As HtmlElementCollection = .GetElementsByTagName("DIV")
                    For Each item In itemlist 'look at each item in the collection
                    If item.classname = "description" Then 
                        msgbox item.innertext 'this would msgbox your description
                    Exit For 'exit once found
                    End If

                Next 'do this for every item in the collection until we find it
            End With 
        wbc.dispose()
        End Using
    End Using 

如果你想告诉我这个页面,我可以更新代码,但这至少有助于回答你的问题。

最新更新