返回列表 上一主題 發帖

[發問] 無法用外部資料匯入的網站能用VBA讀取資料嗎?

回復  小俠客 Set A = .Document.getElementsByTAGName("TD")
不放在 Do   Loop 迴圈也可以,
但因網頁資料 ...
GBKEE 發表於 2014-6-20 21:03


原來如此,因為我是用「逐行執行」的方式研究代碼,所以比較沒有這個問題,實際運行確需要作這個checking。
那麼a.length = 84又是甚麼意思?

A = 「返回帶有TD標簽名的對象集合」
A.Length 是指這個集合的總數
但我參考 「Private Sub 網頁的元素()」
我發現只有54個TD ......

TOP

本帖最後由 GBKEE 於 2014-6-23 09:43 編輯

回復 11# 小俠客
  1. '網頁搜尋結果: 0 記錄
  2. Option Explicit
  3. Sub IE下一頁()
  4.     Dim url As String, a As Object, Table As Object, i As Integer, pubSrch As Object, Pages As Integer
  5.     Dim Sh As Worksheet, B As String, E
  6.     Set Sh = ActiveSheet
  7.     Sh.Cells.Clear
  8.     '************************************
  9.     url = "https://eservices.customs.gov.hk/MSOS/wsrh/001s1?"  
  10.    'url = "https://eservices.customs.gov.hk/MSOS/wsrh/001s1?"searchBy=A"  '網頁有資料
  11.     '************************************
  12.     With CreateObject("InternetExplorer.Application")
  13.         .Visible = True     '  是否顯示 IE
  14.         .Navigate url
  15.         Do While .ReadyState <> 4 Or .Busy
  16.             DoEvents
  17.         Loop
  18.         Do
  19.             Set a = .Document.getElementsByTAGName("TD")
  20.         Loop Until Not a Is Nothing 'And a.Length = 84
  21.         On Error Resume Next
  22.         With Sh
  23.             For i = 0 To a.Length - 1
  24.                 .Cells(i + 1, "A") = i
  25.                 .Cells(i + 1, "B") = a(i).Type
  26.                 .Cells(i + 1, "C") = a(i).ID
  27.                 .Cells(i + 1, "D") = a(i).InnerText
  28.                 .Cells(i + 1, "E") = a(i).Value
  29.             Next
  30.             
  31.         End With
  32.         
  33.         .Quit
  34.     End With
  35.     MsgBox "getElementsByTAGName(""TD"") 合計 " & a.Length
  36. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 好事要提得起,是非要放得下,成就別人即是成就自己。
返回列表 上一主題