返回列表 上一主題 發帖

[發問] 請問 如何用 vba 點擊網頁查詢

回復 1# GaryC
  1. With .Document
  2.             .all("myear").Value = Year(xDate) - 1913 '日期可修改
  3.            MsgBox .getElementsByName("myear")(0).Value
  4.             .all("mmon").Value = Format(Month(xDate) - 1, "0#")
  5.             For Each E In .GetElementsByTagName("INPUT")
  6.                 If E.Value = "查詢" Then E.Click
  7.             Next
  8.         End With
複製代碼
每一個網頁(HTM )的建置都不一樣.
  1. Option Explicit
  2. Sub Ex_網頁元素()
  3.     Dim i As Integer, E As Object
  4.     With CreateObject("InternetExplorer.Application")
  5.         .Visible = True
  6.         .Navigate "http://www.twse.com.tw/ch/trading/indices/MI_5MINS_HIST/MI_5MINS_HIST.php"
  7.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  8.         With .Document
  9.           '*** 這段程式碼可查看這網頁的元素內容
  10.             On Error Resume Next
  11.             For Each E In .all
  12.                 i = i + 1
  13.                 Cells(i, "a") = E.tagname '使用 .GetElementsByTagName("INPUT")
  14.                 Cells(i, "b") = E.ID      '使用 .GetElementByID("menu")
  15.                 Cells(i, "c") = E.Name    '使用 .all("myear")
  16.                                           '.getElementsByName("myear")(0).Value ' = Year(xDate) - 1911
  17.                                           ' HTML 中通常具有相同的 name 屬性),所有 getElementsByName() 方法返回的是元素的集合,而不是單一個元素。
  18.                 Cells(i, "d") = E.Value
  19.                 Cells(i, "e") = E.innertext
  20.                 Cells(i, "f") = E.Type
  21.                 Cells(i, "g") = E.href
  22.             Next
  23.         End With
  24.     '    .Quit        '關閉網頁
  25.     End With
  26. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 人的心地是一畦田,土地沒有播下好種子,也長不出好的果實。 -
返回列表 上一主題