返回列表 上一主題 發帖

[發問] 請問下載融資融卷網頁

回復 1# ashin1981
試試看
  1. Option Explicit
  2. Sub Ex()
  3.         Dim xDate As String, hTable As Object, i As Integer, j As Integer
  4.         xDate = "2016/01/14"
  5.         With CreateObject("InternetExplorer.Application")
  6.             .Visible = True
  7.             .Navigate "http://www.twse.com.tw/ch/trading/exchange/MI_MARGN/MI_MARGN.php"
  8.             Do While .Busy Or .readyState <> 4: DoEvents: Loop
  9.             .Document.getElementById("date-field").Value = Format(xDate, "EE/MM/DD") '填入
  10.             '---------,(IE8 版本使用內建的fireevent 的onchange),高於IE8版本改用調用js的方法--------
  11.             '   Set evt = .Document.createEvent("HTMLEvents")
  12.             '   evt.initEvent "change", True, False
  13.             ' Set lst = .Document.all("selectType") 'option的name是selectType,但getElementsByName無法處理,要all才行
  14.             '   lst.selectedIndex = 0
  15.             '   lst.dispatchEvent evt
  16.             .Document.all("query-button").Click
  17.             '*****Application.Wait Now + TimeValue("00:00:5") ****
  18.             Do While .Busy Or .readyState <> 4: DoEvents: Loop
  19.             Do:    DoEvents:     Loop Until InStr(.Document.body.outerText, Format(xDate, "EE年MM月DD日") & Space(1) & "信用交易統計")
  20.             '網頁已更新為查尋日期的資料
  21.             Do
  22.                 Do
  23.                     Set hTable = .Document.getElementsByTagName("table")(3) '第4個table
  24.                 Loop Until Not hTable Is Nothing
  25.             Loop Until hTable.Rows.Length = 5  '取得完整物件
  26.             '*********    用迴圈 取代  Application.Wait (等候)********
  27.             With ActiveSheet
  28.                 .UsedRange.Clear
  29.                 For i = 1 To hTable.Rows.Length - 1 '前3個是標題與空白跳過
  30.                 'For i = 0 To hTable.Rows.Length - 1 '標題開始
  31.                     For j = 0 To hTable.Rows(i).Cells.Length - 1
  32.                         .Cells(i, j + 1) = hTable.Rows(i).Cells(j).innertext  ''前3個是標題與空白跳過
  33.                        ' .Cells(i + 1, j + 1) = hTable.Rows(i).Cells(j).innertext
  34.                     Next
  35.                 Next
  36.             End With
  37.             .Quit
  38.             MsgBox "OK"
  39.     End With
  40. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 3# ashin1981

'---------這段程式碼需用在高於IE8版本,IE8版不適用這段程式碼 --------
  1.     Set evt = .Document.createEvent("HTMLEvents")
  2.     evt.initEvent "change", True, False
  3.     Set lst = .Document.all("selectType") 'option的name是selectType,但getElementsByName無法處理,要all才行
  4.     lst.selectedIndex = 0
  5.     lst.dispatchEvent evt
  6.     '-----------------------
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 脾氣嘴巴不好,心地再好也不能算是好人。
返回列表 上一主題