Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 运行时错误91,未设置对象变量或带块变量_Vba_Web Scraping_Xmlhttprequest_Web Crawler - Fatal编程技术网

Vba 运行时错误91,未设置对象变量或带块变量

Vba 运行时错误91,未设置对象变量或带块变量,vba,web-scraping,xmlhttprequest,web-crawler,Vba,Web Scraping,Xmlhttprequest,Web Crawler,我的意图是从该页面和通向下一页面的应用程序链接中删除所有应用程序名称。然而,当我运行它时,我看到在循环之后,它会产生以下错误“运行时错误91,对象变量或未设置块变量”。下面是完整的代码。任何帮助都将不胜感激。提前谢谢 Sub app_crawler() Dim xmlpage As New XMLHTTP60, htmldoc As New HTMLDocument Dim htmlas As Object, htmla As Object, sstr As String

我的意图是从该页面和通向下一页面的应用程序链接中删除所有应用程序名称。然而,当我运行它时,我看到在循环之后,它会产生以下错误“运行时错误91,对象变量或未设置块变量”。下面是完整的代码。任何帮助都将不胜感激。提前谢谢

Sub app_crawler()
    Dim xmlpage As New XMLHTTP60, htmldoc As New HTMLDocument
    Dim htmlas As Object, htmla As Object, sstr As String

    xmlpage.Open "GET", "https://itunes.apple.com/us/app/candy-crush-saga/id553834731?mt=8", False
    xmlpage.send
    htmldoc.body.innerHTML = xmlpage.responseText

    For Each htmlas In htmldoc.getElementsByClassName("lockup-info")(0).getElementsByTagName("a")
        sstr = htmlas.href

        xmlpage.Open "GET", sstr, False
        xmlpage.send
        htmldoc.body.innerHTML = xmlpage.responseText

        For Each htmla In htmldoc.getElementsByClassName("intro")(1).getElementsByTagName("h1")
            x = x + 1: Cells(x, 1) = htmla.innerText
        Next htmla
    Next htmlas
End Sub
同样,在从中获取元素之前,您必须测试
htmlas(x)
是否返回
Nothing
,这同样适用于
getElementByTagName
和其他:

Sub TestSth()
  Dim xmlpage As New MSXML2.XMLHTTP60
  Dim htmldoc As New MSHTML.HTMLDocument
  Dim htmlas As Object, gist As Object
  Dim htmla As Object, main As Object, lux As String
  Dim x As Long, link As Object, thank As Object
  Range("A1").Select
  xmlpage.Open "GET", "https://itunes.apple.com/us/app/candy-crush-saga/id553834731?mt=8", False
  xmlpage.send
  htmldoc.body.innerHTML = xmlpage.responseText
  Set xmlpage = Nothing

  Set htmlas = htmldoc.getElementsByClassName("lockup-info")
  For x = 0 To htmlas.Length - IIf(htmlas.Length > 0, 1, 0)
    If Not htmlas(x) Is Nothing Then
      If Not htmlas(x).getElementsByTagName("a") Is Nothing Then
        If Not htmlas(x).getElementsByTagName("a")(0) Is Nothing Then
          lux = htmlas(x).getElementsByTagName("a")(0).getAttribute("href")
          If lux <> "" Then
            xmlpage.Open "GET", lux, False
            xmlpage.send
            htmldoc.body.innerHTML = xmlpage.responseText

            Set main = htmldoc.getElementsByClassName("intro")(1)
            Set thank = main.getElementsByTagName("div")
            For Each link In thank
              ActiveCell.Value = link.innertext
              ActiveCell.Offset(1, 0).Select
            Next link
          End If
        End If
      End If
    End If
  Next x
End Sub
subteststh()
Dim xmlpage作为新的MSXML2.XMLHTTP60
将htmldoc设置为新的MSHTML.HTMLDocument
Dim htmlas作为对象,gist作为对象
Dim htmla作为对象,main作为对象,lux作为字符串
尺寸x尽可能长,链接为对象,感谢为对象
范围(“A1”)。选择
xmlpage.Open“GET”https://itunes.apple.com/us/app/candy-crush-saga/id553834731?mt=8”“错
xmlpage.send
htmldoc.body.innerHTML=xmlpage.responseText
设置xmlpage=Nothing
设置htmlas=htmldoc.getElementsByClassName(“锁定信息”)
对于x=0到htmlas.Length-IIf(htmlas.Length>0,1,0)
如果不是,那么htmlas(x)什么都不是
如果不是htmlas(x),那么getElementsByTagName(“a”)什么都不是
如果不是htmlas(x),那么getElementsByTagName(“a”)(0)什么都不是
lux=htmlas(x).getElementsByTagName(“a”)(0.getAttribute(“href”)
如果是“lux”,那么
xmlpage.Open“GET”,lux,False
xmlpage.send
htmldoc.body.innerHTML=xmlpage.responseText
Set main=htmldoc.getElementsByClassName(“简介”)(1)
Set thank=main.getElementsByTagName(“div”)
感谢中的每个链接
ActiveCell.Value=link.innertext
ActiveCell.Offset(1,0)。选择
下一环节
如果结束
如果结束
如果结束
如果结束
下一个x
端接头

这是解决我所有问题的答案:

Sub app_crawler()
    Dim http As New XMLHTTP60, hdoc As New HTMLDocument, hdoc_one As New HTMLDocument
    Dim elem As Object, post As Object, sstr As String

    With http
        .Open "GET", "https://itunes.apple.com/us/app/candy-crush-saga/id553834731?mt=8", False
        .send
        hdoc.body.innerHTML = .responseText
    End With

    For Each elem In hdoc.getElementsByClassName("lockup-info")
        With elem.getElementsByTagName("li")(0).getElementsByTagName("a")
            If .Length Then sstr = .Item(0).href
        End With
        With http
            .Open "GET", sstr, False
            .send
            hdoc_one.body.innerHTML = .responseText
        End With

        For Each post In hdoc_one.getElementsByClassName("intro")
            With post.getElementsByTagName("h1")
                If .Length Then i = i + 1: Cells(i, 1) = .Item(0).innerText
            End With
        Next post
    Next elem
End Sub 

你在哪一行得到了错误?我只是复制了完整的代码并粘贴在这里,以避免混淆我的问题-不幸的是,你基本上给了我们一个过程并说“它不起作用”,而没有给我们任何关于问题可能出在哪里的指示,或者表现出任何形式的努力,试图将问题隔离到一个特定的指令中。你能试着这样做和你的问题吗?你需要在你的问题中加入这些信息。而且,你的问题可能已经被回答了数百次了(只需看看侧栏中所有“相关”的问题)。你知道如果
Foo
Nothing
,你就不能做
Foo.Bar(“abc”)
,对吗?分离链接调用,处理
htmlas(x)
返回
Nothing
的情况,然后处理
getElementsByTagName(“a”)
返回
Nothing
的情况。非常抱歉这个错误。这是我在这里的第一个问题,所以我不知道该怎么问。谢谢你的回复。谢谢你的努力,但它并没有收集到它应该收集的全部数据。我昨晚已经解决了这个问题。以下是我所期待的: