- 帖子
- 4
- 主題
- 1
- 精華
- 0
- 積分
- 6
- 點名
- 0
- 作業系統
- WIN7
- 軟體版本
- Office 2010
- 閱讀權限
- 10
- 性別
- 男
- 來自
- Hsinchu
- 註冊時間
- 2016-1-4
- 最後登錄
- 2017-4-12
|
本帖最後由 Hunt01 於 2016-1-11 17:16 編輯
非常感謝 joey0415 以及 stillfish00 大大的指導 :$
小弟利用上週及假日胡亂摸索,初步只大概會用 onclick 來達到目的, xmlhttp 後續再接再勵。
不過奇怪的是,小弟 VBA click 時,好像會因為網頁 refresh 不及而造成後續抓取資料步驟產生錯誤。我有加入 do loop 來等待,不過看來這個 do loop 無作用。
大大們有建議的處理方法嗎?
附上小弟拙劣的 code 供參考。
謝謝大家!!!!
Dim IE As New InternetExplorer
IE.Visible = True
'重大訊息公佈網頁
IE.Navigate "http://mops.twse.com.tw/mops/web/t05sr01_1"
Do: DoEvents: Loop Until IE.readyState = READYSTATE_COMPLETE
Dim doc As HTMLDocument
Set doc = IE.document
'按下"詳細資料" button
Set docinput = doc.getElementsByTagName("input")
r = 1
For i = 0 To docinput.Length - 1
If docinput(i).Value = "詳細資料" And docinput(i).Type = "button" Then
'???按下"詳細資料"button, 有時有問題???
docinput(i).Click
Do: DoEvents: Loop Until IE.readyState = READYSTATE_COMPLETE
'進入"詳細資料"頁面後,開始抓取主要內容。主要訊息內容為 table(9)
Set A = doc.getElementsByTagName("table")(9)
Do: DoEvents: Loop Until IE.readyState = READYSTATE_COMPLETE
'將主要訊息內容放入 sheet
For j = 1 To A.Rows(4).Cells.Length - 1
Sheets(1).Cells(r, j + 1).Select
Sheets(1).Cells(r, j + 1).Value = A.Rows(4).Cells(j).innerText
Next j
r = r + 1
'抓完訊息內容後,返回上一頁
For Each img In doc.getElementsByTagName("img")
If img.href = "http://mops.twse.com.tw/mops/web/images/bu_05.gif" Then
'???img.click 有時無動作???
img.Click
Do: DoEvents: Loop Until IE.Busy = False And IE.readyState = READYSTATE_COMPLETE
Exit For
End If
Next
End If
Next i
IE.Quit |
|