[英]438 error VBA excel scraping from hyperlinks
我正在嘗試從某些網頁上抓取文字,因此使用了以下代碼。 目前,我已經設置了excel,以便我在A列中放入一個超鏈接,並且它循環瀏覽並將所有文本從網頁中拉到excel中。 但是,我不斷得到
438錯誤
在ie.document.body.innertext
行上。
有誰知道為什么會這樣? 我進行了相當多的搜索,但是我並沒有取得太大的成功,因為這似乎是一個相當普遍的問題,但是原因很多。 任何幫助將不勝感激,對於任何明顯的事情,我會事先道歉,因為我對VBA還是很陌生。
Sub Sample()
Dim ie As Object
Dim retStr As String
Dim sht As Worksheet
Dim LastRow As Long
Dim rCell As Range
Dim rRng As Range
Set sht = ThisWorkbook.Worksheets("Sheet1")
'Ctrl + Shift + End
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
Set ie = CreateObject("internetexplorer.application")
Set rRng = Sheet1.Range("A1:A" & LastRow)
For Each rCell In rRng.Cells
With ie
.Navigate rCell.Value
.Visible = True
End With
Do While ie.readystate <> 4: Wait 5: Loop
DoEvents
rCell.Offset(0, 1).Value = ie.document.body.innerText
Next rCell
End Sub
Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
While nSec > Timer
DoEvents
Wend
End Sub
您真的需要IE來完成這項工作嗎? 也許更好地嘗試HTTP請求?
Dim xHttp As MSXML2.XMLHTTP
Dim hDoc As MSHTML.HTMLDocument
Set rRng = Sheet1.Range("A1:A" & LastRow)
For Each rCell In rRng.Cells
Set xHttp = New MSXML2.XMLHTTP
xHttp.Open "GET", rCell.Value
xHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xHttp.send
Do Until xHttp.READYSTATE = 4
DoEvents
Loop
If xHttp.Status = 200 Then
rCell.Offset(0, 1).Value = xHttp.responseText
End If
Next rCell
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.