剛好有一個需求是要去讀網頁資訊
之前就有寫過類似的東東....
就再研究一下
這個需求是參考下列這二個地方的資訊
中央銀行
http://www.cbc.gov.tw/lp.asp?CtNode=645&CtUnit=308&BaseDSD=32&mp=1
海關三旬
http://web.customs.gov.tw/currency/currency/currency.asp?qry=1&language=c
查一下有好多的相關範例可以參考
就參考一下人家怎麼做改成自己的方式
下面就是結果啦....
Imports System
Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Xml
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'央行
Dim CentralBankURL As String = "http://www.cbc.gov.tw/lp.asp?CtNode=645&CtUnit=308&BaseDSD=32&mp=1"
'海關
Dim InquiryURL As String = "http://web.customs.gov.tw/currency/currency/current.txt"
Try
'Call CentralBank(CentralBankURL)
Call InquiryRate(InquiryURL)
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
Me.Dispose()
Me.Close()
End Try
End Sub
Private Function CentralBank(ByVal CentralBankURL As String)
Dim tempStr As String
Dim request As Net.WebRequest = WebRequest.Create(CentralBankURL)
Dim response As WebResponse = request.GetResponse()
Dim stream As IO.Stream = response.GetResponseStream()
Dim sr As StreamReader
Dim doc As XmlDocument = New XmlDocument
request.Method = "POST"
sr = New StreamReader(stream, Encoding.GetEncoding("UTF-8"))
tempStr = sr.ReadToEnd
'有直接看了一下原始碼央行的匯率應該是Gridview產生
'轉Html就變成Table了...有查到
'http://itgroup.blueshop.com.tw/moan/it_club?n=convew&i=143918
'他是參考:http://blog.stevenlevithan.com/archives/match-innermost-html-element
'直接就只取裡面所有的資訊回來再用xml的方式一個一個取出來.....
'不知道有沒有更好的方式就是了.....
Dim ex As New RegularExpressions.Regex("]*>(?:(?=([^<]+))\1|<(?!table\b[^>]*>))*?")
For Each m As RegularExpressions.Match In ex.Matches(tempStr)
For Each c As RegularExpressions.Capture In m.Captures
doc.LoadXml(c.Value)
Dim root As XmlNode = doc.FirstChild
If root.HasChildNodes Then
Dim i As Integer
For i = 0 To root.ChildNodes.Count - 1
MessageBox.Show(root.ChildNodes.Item(i).ChildNodes.Item(0).InnerText & "---" & root.ChildNodes.Item(i).ChildNodes.Item(1).InnerText)
Next i
End If
Next
Next
sr.Close()
sr.Dispose()
Return ""
End Function
Private Function InquiryRate(ByVal InquiryURL As String)
Dim request As Net.WebRequest = WebRequest.Create(InquiryURL)
Dim response As WebResponse = request.GetResponse()
Dim stream As IO.Stream = response.GetResponseStream()
Dim sr As StreamReader
request.Method = "POST"
sr = New StreamReader(stream, Encoding.Default)
Dim strSr As String = ""
Do While sr.Peek() >= 0
'有遇到一個狀況就是海關有提供txt File不過裡面的分隔符號是用Tab(想說奇怪怎麼都Split錯)
'後來查了一下:http://www.knowdotnet.com/articles/controlchars.html才發現是這樣ControlChars.Tab
MessageBox.Show(sr.ReadLine().ToString().Split(ControlChars.Tab).GetValue(0) & "---" & _
sr.ReadLine().ToString().Split(ControlChars.Tab).GetValue(1) & "---" & _
sr.ReadLine().ToString().Split(ControlChars.Tab).GetValue(2) & "---" & _
sr.ReadLine().ToString().Split(ControlChars.Tab).GetValue(3) & "---" & _
sr.ReadLine().ToString().Split(ControlChars.Tab).GetValue(4) & "---" & _
sr.ReadLine().ToString().Split(ControlChars.Tab).GetValue(5))
Loop
Return ""
End Function
文章標籤
全站熱搜
