【问题标题】:VBA code to get Near By location from url to excelVBA代码从url获取附近位置到excel
【发布时间】:2018-06-12 18:19:20
【问题描述】:

我正在尝试从 url 获取连接、基础设施和邻域信息到 excel 下面是我尝试过的 vba 代码,但我收到错误-运行时错误“91”:对象变量或未设置块变量。

Sub test()
Dim oHtml As HTMLDocument
Dim oElement As Object
Set oHtml = New HTMLDocument
With CreateObject("WINHTTP.WinHTTPRequest.5.1")
.Open "GET", "https://www.99acres.com/dasnac-the-jewel-of-noida-sector-75-noida-npxid-r14046?src=NPSRP&sid=UiB8IFFTIHwgUyB8IzEjICB8IG5vaWRhIzUjIHwgQ1AyNSB8IFkgIzE4I3wgIHwgMjUgfCMzIyAgfCA3ICM1I3wgIHwgMjMgfCM0MyMgIHw="
.Send
oHtml.body.innerHTML = .responseText
End With
ele3 = oHtml.getElementById("locHighlightsSlide")
Range("A1") = ele3.innerText
End Sub

下面是我的html代码

<div class="vsp-SliderBar" id="locHighlightsSlide">
                    <div class="amnLeftScroll noVis amenitiesScrollArrow dev_leftlocHightlights">        
                        <i class="xid_icons xid_icons_1x xid_leftarrow1x"></i>            
                    </div>
                    <div class="vsp-container">
                        <div class="vsp-itemsFilm clearAfter animTransMrg350">
                                                <div class="vsp-item highlightsCard">
                            <div class="highlightsCardHeading">Connectivity</div>
                                                            <div class="highlightsCardRow jsinteraction" style="overflow: visible;">
                                    <div class="highlightsCardCol">
                                        <i class="xid_icons xid_icons_1x xid_loc_metro"></i>
                                    </div>
                                    <div class="highlightsCardCol jsinteraction" style="overflow: visible;">
                                        <span class="highlightPoint">Wave City Centr...</span>
                                        <span class="overflowItem" style="top:auto;z-index: 1">Wave City Centre</span>                                                                            </div>
                                    <div class="highlightsCardCol">4 km | 20 min</div>
                                </div>
                                                            <div class="highlightsCardRow jsinteraction" style="overflow: visible;">
                                    <div class="highlightsCardCol">
                                        <i class="xid_icons xid_icons_1x xid_loc_metro"></i>
                                    </div>
                                    <div class="highlightsCardCol jsinteraction" style="overflow: visible;">
                                        <span class="highlightPoint">Golf Course Sta...</span>
                                        <span class="overflowItem" style="top:auto;z-index: 1">Golf Course Station</span>                                                                            </div>
                                    <div class="highlightsCardCol">5 km | 25 min</div>
                                </div>
                                                    </div>
                        <div class="vsp-item highlightsCard" id="viewMore_Connectivity" style="display: none;">
                                                            <div class="highlightsCardRow jsinteraction rel" style="overflow: visible;">
                                    <div class="highlightsCardCol">
                                        <i class="xid_icons xid_icons_1x xid_loc_metro"></i>
                                    </div>
                                    <div class="highlightsCardCol jsinteraction" style="overflow: visible;">
                                        <span class="highlightPoint">Wave City Centr...</span>
                                        <span class="overflowItem" style="top:auto;z-index: 1">Wave City Centre</span>                                                                            </div>
                                    <div class="highlightsCardCol">4 km | 20 min</div>
                                </div>
                                                            <div class="highlightsCardRow jsinteraction rel" style="overflow: visible;">
                                    <div class="highlightsCardCol">
                                        <i class="xid_icons xid_icons_1x xid_loc_metro"></i>
                                    </div>
                                    <div class="highlightsCardCol jsinteraction" style="overflow: visible;">
                                        <span class="highlightPoint">Golf Course Sta...</span>
                                        <span class="overflowItem" style="top:auto;z-index: 1">Golf Course Station</span>                                                                            </div>
                                    <div class="highlightsCardCol">5 km | 25 min</div>
                                </div>
                                                    </div>
                                                <div class="vsp-item highlightsCard">
                            <div class="highlightsCardHeading">Infrastructure</div>
                                                            <div class="highlightsCardRow jsinteraction" style="overflow: visible;">
                                    <div class="highlightsCardCol">
                                        <i class="xid_icons xid_icons_1x xid_loc_education"></i>
                                    </div>
                                    <div class="highlightsCardCol jsinteraction" style="overflow: visible;">
                                        <span class="highlightPoint">Manav Rachna In...</span>
                                        <span class="overflowItem" style="top:auto;z-index: 1">Manav Rachna International School</span>                                                                            </div>
                                    <div class="highlightsCardCol">2 km | 15 min</div>
                                </div>
                                                            <div class="highlightsCardRow jsinteraction" style="overflow: visible;">
                                    <div class="highlightsCardCol">
                                        <i class="xid_icons xid_icons_1x xid_loc_education"></i>
                                    </div>
                                    <div class="highlightsCardCol jsinteraction" style="overflow: visible;">
                                        <span class="highlightPoint">Global Indian I...</span>
                                        <span class="overflowItem" style="top:auto;z-index: 1">Global Indian International School</span>                                                                            </div>
                                    <div class="highlightsCardCol">2 km | 15 min</div>
                                </div>
                                                            <div class="highlightsCardRow jsinteraction" style="overflow: visible;">
                                    <div class="highlightsCardCol">
                                        <i class="xid_icons xid_icons_1x xid_loc_health"></i>
                                    </div>
                                    <div class="highlightsCardCol jsinteraction" style="overflow: visible;">
                                        <span class="highlightPoint">Neo Hospital</span>
                                                                                                                    </div>
                                    <div class="highlightsCardCol">3 km | 15 min</div>
                                </div>
                                                    </div>
                        <div class="vsp-item highlightsCard" id="viewMore_Infrastructure" style="display: none;">
                                                            <div class="highlightsCardRow jsinteraction rel" style="overflow: visible;">
                                    <div class="highlightsCardCol">
                                        <i class="xid_icons xid_icons_1x xid_loc_education"></i>
                                    </div>
                                    <div class="highlightsCardCol jsinteraction" style="overflow: visible;">
                                        <span class="highlightPoint">Manav Rachna In...</span>
                                        <span class="overflowItem" style="top:auto;z-index: 1">Manav Rachna International School</span>                                                                            </div>
                                    <div class="highlightsCardCol">2 km | 15 min</div>
                                </div>
                                                            <div class="highlightsCardRow jsinteraction rel" style="overflow: visible;">
                                    <div class="highlightsCardCol">
                                        <i class="xid_icons xid_icons_1x xid_loc_education"></i>
                                    </div>
                                    <div class="highlightsCardCol jsinteraction" style="overflow: visible;">
                                        <span class="highlightPoint">Global Indian I...</span>
                                        <span class="overflowItem" style="top:auto;z-index: 1">Global Indian International School</span>                                                                            </div>
                                    <div class="highlightsCardCol">2 km | 15 min</div>
                                </div>
                                                            <div class="highlightsCardRow jsinteraction rel" style="overflow: visible;">
                                    <div class="highlightsCardCol">
                                        <i class="xid_icons xid_icons_1x xid_loc_health"></i>
                                    </div>
                                    <div class="highlightsCardCol jsinteraction" style="overflow: visible;">
                                        <span class="highlightPoint">Neo Hospital</span>
                                                                                                                    </div>
                                    <div class="highlightsCardCol">3 km | 15 min</div>
                                </div>
                                                    </div>
                                                <div class="vsp-item highlightsCard">
                            <div class="highlightsCardHeading">Neighborhood</div>
                                                            <div class="highlightsCardRow jsinteraction" style="overflow: visible;">
                                    <div class="highlightsCardCol">
                                        <i class="xid_icons xid_icons_1x xid_loc_village"></i>
                                    </div>
                                    <div class="highlightsCardCol jsinteraction" style="overflow: visible;">
                                        <span class="highlightPoint">Sector-72</span>
                                                                                                                    </div>
                                    <div class="highlightsCardCol">3 km | 15 min</div>
                                </div>
                                                            <div class="highlightsCardRow jsinteraction" style="overflow: visible;">
                                    <div class="highlightsCardCol">
                                        <i class="xid_icons xid_icons_1x xid_loc_village"></i>
                                    </div>
                                    <div class="highlightsCardCol jsinteraction" style="overflow: visible;">
                                        <span class="highlightPoint">Sector-76</span>
                                                                                                                    </div>
                                    <div class="highlightsCardCol">3 km | 15 min</div>
                                </div>
                                                            <div class="highlightsCardRow jsinteraction" style="overflow: visible;">
                                    <div class="highlightsCardCol">
                                        <i class="xid_icons xid_icons_1x xid_loc_village"></i>
                                    </div>
                                    <div class="highlightsCardCol jsinteraction" style="overflow: visible;">
                                        <span class="highlightPoint">Sector-51</span>
                                                                                                                    </div>
                                    <div class="highlightsCardCol">3 km | 15 min</div>
                                </div>
                                                    </div>
                        <div class="vsp-item highlightsCard" id="viewMore_Neighborhood" style="display: none;">
                                                            <div class="highlightsCardRow jsinteraction rel" style="overflow: visible;">
                                    <div class="highlightsCardCol">
                                        <i class="xid_icons xid_icons_1x xid_loc_village"></i>
                                    </div>
                                    <div class="highlightsCardCol jsinteraction" style="overflow: visible;">
                                        <span class="highlightPoint">Sector-72</span>
                                                                                                                    </div>
                                    <div class="highlightsCardCol">3 km | 15 min</div>
                                </div>
                                                            <div class="highlightsCardRow jsinteraction rel" style="overflow: visible;">
                                    <div class="highlightsCardCol">
                                        <i class="xid_icons xid_icons_1x xid_loc_village"></i>
                                    </div>
                                    <div class="highlightsCardCol jsinteraction" style="overflow: visible;">
                                        <span class="highlightPoint">Sector-76</span>
                                                                                                                    </div>
                                    <div class="highlightsCardCol">3 km | 15 min</div>
                                </div>
                                                            <div class="highlightsCardRow jsinteraction rel" style="overflow: visible;">
                                    <div class="highlightsCardCol">
                                        <i class="xid_icons xid_icons_1x xid_loc_village"></i>
                                    </div>
                                    <div class="highlightsCardCol jsinteraction" style="overflow: visible;">
                                        <span class="highlightPoint">Sector-51</span>
                                                                                                                    </div>
                                    <div class="highlightsCardCol">3 km | 15 min</div>
                                </div>
                                                    </div>
                                                </div>
                    </div>
                    <div class="amnRightScroll amenitiesScrollArrow dev_rightLocHighlights">                  
                        <i class="xid_icons xid_icons_1x xid_rightarrow1x dev_vsp-Next"></i>            
                    </div>
                </div>

请在下面的图片中找到我需要的数据。

【问题讨论】:

  • 您需要从这些字段中获得哪些信息:徽标?您甚至没有必要澄清您的输出可能是怎样的问题。

标签: vba web-scraping msxml


【解决方案1】:

您可以使用 CSS 选择器,它们是用于选择元素的模式。对于您的 HTML 示例模式,可以使用:

  1. 第一列:.vsp-item.highlightsCard:first-child
  2. 第二列:.vsp-item.highlightsCard:nth-child(2)
  3. 第三列:.vsp-item.highlightsCard:last-child
  4. 标题:.highlightsCardHeading

上述选择器中的.class selector,即通过className 选择元素。


CSS 查询结果

第 1 列连接性:

第 2 列基础设施:

第 3 列社区:

标题:

观察:上面的查询结果表明Columns 1-3需要单个节点,即.querySelector;请注意,0 是唯一返回的匹配项。而Headers 需要.querySelectorAll,返回一个nodeList,我们可以看到0-2 返回。


VBA:

我无法访问该网站进行查询,但 .querySelector 方法属于 HTMLDocument 并且通常是(使用您的 HTMLDocument 变量 oHTML):

对于单个节点:

oHtml.querySelector(".vsp-item.highlightsCard:first-child")

对于节点列表:

oHtml.querySelectorAll(".highlightsCardHeading")

如果是 nodeList,您可以使用以下命令迭代其项目:

Dim headerNodeList As Object, i As Long
Set headerNodeList = oHtml.querySelectorAll(".highlightsCardHeading")

For i = 0 To headerNodeList.Length - 1
    Debug.Print headerNodeList.item(i).innerText '<==this way          '
   'Debug.Print headerNodeList(i).innerText    '<==Or this method
Next i

对于单个元素,请使用 .querySelector.innerText

Dim col1Node As Object: Set col1Node = oHtml.querySelector(".vsp-item.highlightsCard:first-child"): Debug.Print col1Node.innerText
Dim col2Node As Object: Set col2Node = oHtml.querySelector(".vsp-item.highlightsCard:nth-child(2)"): Debug.Print col2Node.innerText
Dim col3Node As Object: Set col3Node = oHtml.querySelector(".vsp-item.highlightsCard:last-child") : Debug.Print col3Node.innerText 

注意事项:

我无法从您的请求中检查.responseText(或者更确切地说,由于与站点的连接失败,它没有意义)。此方法可能不会返回所需的信息,特别是对于动态加载的页面。不过,我相信WinHTTPRequest.5.1 具有您可以使用的响应状态。

在这种情况下,您可能需要使用其他方法,例如基于 IE 浏览器并等待页面加载。你仍然会使用.querySelector.querySelectorAllHTMLDocument 方法。


示例脚本:

  1. IE 的方法在这里:https://stackoverflow.com/a/50613063/6241235
  2. WinHTTP 的方法:https://stackoverflow.com/a/50759945/6241235https://stackoverflow.com/a/50757822/6241235

信息:

  1. HTMLDOM querySelector() method
  2. CSS Selectors

【讨论】:

  • 嗨 QHarr,感谢您的大力帮助!!!您提供的完整信息对我很有帮助!!!!
  • 高兴@saransh
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多