返回列表 上一主題 發帖

使用VBA抓取網頁資料,大約不到5筆就會當掉,求解

回復 1# clio
只抓
https://www.digikey.tw/products/en?keywords=%22CRL2010-JW-1R00ELF-ND%22

剩下請自行修改
test.zip (15.43 KB)

TOP

回復 3# clio

一筆,多筆,無資料,可用網頁上有幾個table判斷

下方是多筆
  1. Sub test()
  2.     Cells.ClearContents
  3.     '設定引用項目  microsoft html object library
  4.     Dim bPostData() As Byte
  5.     Dim URL, strurlHeaders As String
  6.     Dim i, j As Integer
  7.     Dim hDoc As New MSHTML.HTMLDocument
  8.     Dim objIE As Object
  9.     spath = ThisWorkbook.Path
  10.     URL = "https://www.digikey.tw/products/en?keywords=ULN2003AFWG"
  11. '    Stop
  12.      strurlHeaders = "Content-Type: application/x-www-form-urlencoded"
  13.     Set objIE = CreateObject("InternetExplorer.Application")
  14.     With objIE
  15.         .Visible = True
  16.         .Navigate URL, , , bPostData, strurlHeaders
  17.         Do While .readyState <> 4: DoEvents: Loop
  18.         Set hTable = .Document.getElementsByTagName("table")(0)
  19.             With ActiveSheet
  20.                 For i = 0 To hTable.Rows.Length - 1
  21.                     For j = 0 To hTable.Rows(i).Cells.Length - 1
  22.                         .Cells(i + 1, j + 1) = hTable.Rows(i).Cells(j).innerText
  23.                     Next
  24.                 Next
  25.             End With
  26.     End With
  27.     objIE.Quit
  28.     Set objIE = Nothing
  29. End Sub
複製代碼

TOP

回復 5# clio

COUNT_TABLE = .Document.getElementsByTagName("table").Length

如果
COUNT_TABLE =2  多欄
COUNT_TABLE =6  一欄
COUNT_TABLE =0  無資料





TOP

本帖最後由 joey0415 於 2019-8-29 21:10 編輯

回復 7# clio

直接調用Application.Wait
VBA內的Application Object 有 Wait 可供使用,若要暫停1秒,可編碼:
Application.Wait Now + TimeValue("00:00:01")

加上等待時間試試
有的網站要等
秒數不一定

公開資訊站goodinfo等,爬太快都會鎖ip

如果還不行
再請益別的高手

TOP

        靜思自在 : 為人處世要小心細心,但不要「小心眼」。
返回列表 上一主題