1

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>
4

1 回答 1

0

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

 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 

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

于 2013-05-06T00:44:09.743 回答