简体   繁体   English

438错误VBA Excel从超链接中抓取

[英]438 error VBA excel scraping from hyperlinks

I have the below code, as I am trying to scrape the text from some webpages. 我正在尝试从某些网页上抓取文字,因此使用了以下代码。 Currently I have the excel set up such that I put in a hyperlink in column A, and it cycles through and pulls all the text from the web page into excel. 目前,我已经设置了excel,以便我在A列中放入一个超链接,并且它循环浏览并将所有文本从网页中拉到excel中。 However, I keep getting a 但是,我不断得到

438 error 438错误

on the ie.document.body.innertext line. ie.document.body.innertext行上。

Does anyone know why this may be happening? 有谁知道为什么会这样? I have searched around a fair bit, but I have not had much success as it seems this is a fairly common problem but with many different causes. 我进行了相当多的搜索,但是我并没有取得太大的成功,因为这似乎是一个相当普遍的问题,但是原因很多。 Any help would be greatly appreciated, and apologies in advance for anything obvious, as I am rather new to VBA. 任何帮助将不胜感激,对于任何明显的事情,我会事先道歉,因为我对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

Do you really need the IE to do the job? 您真的需要IE来完成这项工作吗? Maybe better try HTTP request? 也许更好地尝试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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM