2020年6月11日 星期四

vb.net 網頁源碼處理 [Get WebPage Source Code]


1
2
3
4
Imports mshtml      'add reference
Imports System.Net
Imports System.IO
Imports System.Windows.Forms    'add reference






1
2
3
4
Imports mshtml      'add reference
Imports System.Net
Imports System.IO
Imports System.Windows.Forms    'add reference


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
    Private Function getHTML(ByVal address As String) As String
        Dim RT As String = ""
        Dim WRequest As WebRequest
        Dim WResponse As WebResponse
        Dim SR As StreamReader

        WRequest = WebRequest.Create(address)
        WResponse = WRequest.GetResponse
        SR = New StreamReader(WResponse.GetResponseStream)
        RT = SR.ReadToEnd()
        SR.Close()
        Return RT
    End Function



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
    Function textFromHtml(ByVal htmlToParse As String) As String
        Dim htmlDocument As IHTMLDocument = New mshtml.HTMLDocument
        Dim sCollect As String = ""

        htmldocument.write(htmlToParse)
        htmldocument.close()
        System.Windows.Forms.MessageBox.Show(htmldocument.title.ToString)

        Dim allElements As IHTMLElementCollection = htmldocument.body.all
        Dim sTags() As String = {"P", "DIV", "SPAN", "H1", "H2", "H3"}
        For Each elem As IHTMLElement In allElements
            Dim sTagUpper As String = elem.tagName.ToUpper()
            If sTags.Contains(sTagUpper) Then
                sCollect += elem.innerText
                If sTagUpper <> "SPAN" Then
                    sCollect += Constants.vbCrLf
                End If
            End If
        Next

        Return sCollect
    End Function



1
2
3
    Public Function stripTags(ByVal htmlToParse As String) As String
        Return Text.RegularExp<b></b>ressions.Regex.Replace(htmlToParse, "<[^>]*>", "")
    End Function

沒有留言:

張貼留言