下面的範例 取自於 http://hotnews.chu.edu.tw/web/newslist.asp 網址
Protected Friend WebBrowserRead As New WebBrowser
Private ListBoxLinkStore As New ListBox
Private DataDate As Date = Nothing, DataTitle As String = Nothing, DataLink As System.Uri = Nothing
'等待網頁讀取完成
Sub Loading(ByRef [web] As WebBrowser)
Do Until WebBrowserRead.ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
Loop
End Sub
'瀏覽網頁
Private Sub ButtonNevigate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNevigate.Click
WebBrowserUser.Navigate(TextBoxUrl.Text)
End Sub
'取得資料
Private Sub GetData()
ListBoxTitle.Items.Clear()
ListBoxLinkStore.Items.Clear()
'擷取Web網頁內的Html項目
For Each [HtmlElement] As HtmlElement In WebBrowserRead.Document.All
'擷取日期
If [HtmlElement].TagName = "SPAN" And IsDate([HtmlElement].OuterText) Then
'<span class="newsdate">2010/12/17</span>
'這就是上面的意思
DataDate = CDate([HtmlElement].OuterText)
End If
'擷取標題和連結
If [HtmlElement].TagName = "A" And Not [HtmlElement].GetAttribute("Href") = Nothing Then
DataTitle = [HtmlElement].GetAttribute("Title")
DataLink = New System.Uri([HtmlElement].GetAttribute("Href"))
'<a class="newstitlelink" href="http://hotnews.chu.edu.tw/web/home/Z30/Z3020101217171513.jpg" title='【學務處】中華感恩九九活動' target="_blank" onMouseOver="window.status='';return true;"><script>newscut('中華感恩九九活動');</script></a> </TD>
'上面的範例 取 title 並且抓 href 當連結
End If
'如果成功蒐集到一筆記錄
If Not DataDate = Nothing AndAlso Not DataTitle = Nothing AndAlso Not DataLink = Nothing Then
ListBoxTitle.Items.Add(DataDate.ToString("yyyy-MM-dd") & "│" & DataTitle)
ListBoxLinkStore.Items.Add(DataLink)
'這行的意思就是在於 上面 取得了 日期並且 有抓到 title 跟 link 之後 就丟去listbox 內
'初始化
DataDate = Nothing
DataTitle = Nothing
DataLink = Nothing
End If
Next
End Sub
Private Sub LoadData()
'載入網頁
WebBrowserRead.Navigate(TextBoxUrl.Text)
'等待網頁載入完成
Loading(WebBrowserRead)
'抓資料
GetData()
End Sub
'讀取網頁內容
Private Sub ButtonReadAnnouncement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReadAnnouncement.Click
LoadData()
End Sub
Private Sub ListBoxTitle_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBoxTitle.SelectedIndexChanged
'叫出網頁
WebBrowserUser.Navigate(ListBoxLinkStore.Items.Item(ListBoxTitle.SelectedIndex))
'等待網頁載入完成
Loading(WebBrowserUser)
End Sub
Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
LoadData()
End Sub
請先 登入 以發表留言。