438错误VBA excel从超链接抓取

时间:2018-04-25 05:50:01

标签: excel-vba web-scraping vba excel

我有以下代码,因为我试图从一些网页中删除文本。目前我有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

1 个答案:

答案 0 :(得分:1)

你真的需要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