[VB.NET]自動從網頁的原始碼取要的資料

以及時取股票漲跌數據為例

 


Option Strict On
Option Explicit On
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions

Public Class F_Main
    Dim dt As DataTable
    Dim dr As DataRow
    Dim ds As DataSet
  
    Private Sub initTable()
        dt = New DataTable
        Dim oCol1 As DataColumn = dt.Columns.Add("stkid", GetType(String))
        oCol1.AllowDBNull = True
        Dim oCol2 As DataColumn = dt.Columns.Add("fill", GetType(String))
        oCol2.AllowDBNull = True
        Dim oCol3 As DataColumn = dt.Columns.Add("price", GetType(String))
        oCol3.AllowDBNull = True
    End Sub

    Private Sub StkSelect(ByVal stk_id As String)
        dr = dt.NewRow
        Dim url As String = "http://tw.stock.yahoo.com/q/q?s=" + stk_id
        Dim StockValues As String = SketchWebPage(url)

        Dim index As Integer = 0
        Dim GetNowTimeHtml As String = ""
        Dim GetStockName As String = "href=""/q/bc?s=" + stk_id + """>"
        index = InStr(StockValues, GetStockName) + GetStockName.Length - 1
        dr(0) = StockValues.Substring(index, StockValues.IndexOf("", index) - index)

        Dim GetFillHtml As String = GetNowTimeHtml + ""
        index = InStr(StockValues, GetFillHtml) + GetFillHtml.Length - 1
        dr(1) = StockValues.Substring(index, StockValues.IndexOf("", index) - index)

        Dim GetPriceHtml As String = ""
        Dim up As String = ""
        Dim down As String = ""
        Dim zero As String = ""

        Select Case InStr(StockValues, GetPriceHtml + up)
            Case 0
                Select Case InStr(StockValues, GetPriceHtml + zero)
                    Case 0
                        Dim dowhtml As String = GetPriceHtml + down
                        index = InStr(StockValues, dowhtml) + dowhtml.Length - 1
                        dr(2) = StockValues.Substring(index, 5)
                    Case Else
                        Dim zerohtml As String = GetPriceHtml + zero
                        index = InStr(StockValues, zerohtml) + zerohtml.Length - 1
                        dr(2) = StockValues.Substring(index, 5)
                End Select
            Case Else
                Dim uphtml As String = GetPriceHtml + up
                index = InStr(StockValues, uphtml) + uphtml.Length - 1
                dr(2) = StockValues.Substring(index, 5)
        End Select
        dt.Rows.Add(dr)
    End Sub

    Private Function SketchWebPage(ByVal URL As String) As String
        Try
            Dim lobjRequest As HttpWebRequest
            Dim lobjResponse As HttpWebResponse
            Dim lobjEncode As Encoding
            Dim lobjStreamReader As StreamReader
            lobjRequest = CType(WebRequest.Create(URL), HttpWebRequest)
            lobjResponse = CType(lobjRequest.GetResponse(), HttpWebResponse)
            lobjEncode = System.Text.Encoding.GetEncoding("big5")
           '建立一個新的stream去做讀取
            lobjStreamReader = New StreamReader(lobjResponse.GetResponseStream, lobjEncode)
      
      Dim stmPage As String = lobjStreamReader.ReadToEnd()
            lobjResponse.Close()
            lobjStreamReader.Close()
            Return stmPage
        Catch ex As Exception
            Return "FAILED"
        End Try
    End Function

    Private Sub btnReload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReload.Click
        initTable()

        StkSelect("2891")
        StkSelect("1532")
        StkSelect("2524")
        StkSelect("2409")
        StkSelect("2704")
        StkSelect("5531")
        StkSelect("5347")
      
        Me.DataGridView1.DataSource = dt
        Me.DataGridView1.Columns(0).Width = 70
        Me.DataGridView1.Columns(1).Width = 60
        Me.DataGridView1.Columns(2).Width = 60
    End Sub
End Class

 範例下載:   Stock.rar