在不使用“on error resume next”的情况下摆脱错误

时间:2017-05-13 16:21:29

标签: vba web-scraping

如果我运行我的代码保持其中的“On error resume next”行,它可以正常工作,但如果我取出这一行,那么它会停止显示错误“对象变量或未设置块变量”。为什么会发生这种情况,如果我想在没有“接下来的错误恢复”的情况下运行我的脚本,那么我该怎么做呢?提前感谢您仔细研究。

Sub CandyCrush()
Dim http As New MSXML2.XMLHTTP60, html As New HTMLDocument
Dim Items As Object, Item As Object

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

Set Items = html.getElementsByClassName("left")
    On Error Resume Next
    For Each Item In Items
        x = x + 1
        Cells(x, 1) = Item.getElementsByTagName("h1")(0).innerText
        Cells(x, 2) = Item.getElementsByTagName("h2")(0).innerText
    Next Item
End Sub

n类标签的元素:

<div class="left">
              <h1 itemprop="name">Candy Crush Saga</h1>
              <h2>By King</h2>
              <div class="editorial-badge">Essentials</div>
            </div>

扩展部分:

Sub RealYP()
Const URL = "https://www.yellowpages.com/search?search_terms=Coffee%20Shops&geo_location_terms=San%20Francisco%2C%20CA&page=2"
Dim html As New HTMLDocument, topics As Object, topic As HTMLHtmlElement

With CreateObject("MSXML2.serverXMLHTTP")
    .Open "GET", URL, False
    .send
    html.body.innerHTML = .responseText
End With
Set topics = html.getElementsByClassName("info")
    On Error Resume Next
    For Each topic In topics
        x = x + 1
        If CBool(topic.getElementsByClassName("track-visit-website").lentgh) Then _
        Cells(x, 1) = topic.getElementsByClassName("track-visit-website")(0).href
    Next topic
End Sub

1 个答案:

答案 0 :(得分:1)

我通常会在尝试访问元素之前检查元素是否存在,并始终定义单元格的父工作表。

with worksheets("sheet1")
    For Each Item In Items
        x = x + 1
        if cbool(Item.getElementsByTagName("h1").length) then _
            .Cells(x, 1) = Item.getElementsByTagName("h1")(0).innerText
        if cbool(Item.getElementsByTagName("h2").length) then _
            .Cells(x, 2) = Item.getElementsByTagName("h2")(0).innerText
    Next Item
end with

如果你运行它并发现A列和B列中有一个空单元格的行,那么就有一个div元素(class =&#34; left&#34;),它不包含H1和H2元素。

A .Length为零意味着没有(基于一个索引)。但是,访问元素是从零开始的索引,因此第一个(如果存在)位于.getElementsByTagName("h2")(0)