VBA 从 url 到 excel 获取附近位置的代码
VBA code to get Near By location from url to excel
我正在尝试获取从 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>
请在下图中找到我需要的数据。
您可以使用 CSS select 或用于 select 元素的模式。对于您的 HTML 有效的示例模式是:
- 第一列:
.vsp-item.highlightsCard:first-child
- 第二列:
.vsp-item.highlightsCard:nth-child(2)
- 第三列:
.vsp-item.highlightsCard:last-child
- Headers 与:
.highlightsCardHeading
.
,在上面的 select 中,是 class selector 即 selecting 元素 className
.
CSS查询结果:
第 1 列连通性:
第 2 列基础结构:
第 3 栏邻居:
Headers:
观察:上面的查询结果表明 Columns 1-3
需要单个节点,即 .querySelector
;请注意 0
是唯一匹配 returned 的。而 Headers
需要 .querySelectorAll
,returning 节点列表,正如我们所见 0-2
returned.
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
(或者更确切地说,由于与站点的连接失败,它没有意义)。此方法可能不会 return 所需的信息,尤其是对于动态加载的页面。不过,我相信 WinHTTPRequest.5.1
有一个您可以使用的响应状态。
在这种情况下,您可能需要使用其他方法,例如基于 IE 浏览器并等待页面加载。您仍然会使用 HTMLDocument
的 .querySelector
或 .querySelectorAll
方法。
示例脚本:
- 此处
IE
的方法:https://whosebug.com/a/50613063/6241235
WinHTTP
的方法:https://whosebug.com/a/50759945/6241235 OR https://whosebug.com/a/50757822/6241235
信息:
我正在尝试获取从 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>
请在下图中找到我需要的数据。
您可以使用 CSS select 或用于 select 元素的模式。对于您的 HTML 有效的示例模式是:
- 第一列:
.vsp-item.highlightsCard:first-child
- 第二列:
.vsp-item.highlightsCard:nth-child(2)
- 第三列:
.vsp-item.highlightsCard:last-child
- Headers 与:
.highlightsCardHeading
.
,在上面的 select 中,是 class selector 即 selecting 元素 className
.
CSS查询结果:
第 1 列连通性:
第 2 列基础结构:
第 3 栏邻居:
Headers:
观察:上面的查询结果表明 Columns 1-3
需要单个节点,即 .querySelector
;请注意 0
是唯一匹配 returned 的。而 Headers
需要 .querySelectorAll
,returning 节点列表,正如我们所见 0-2
returned.
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
(或者更确切地说,由于与站点的连接失败,它没有意义)。此方法可能不会 return 所需的信息,尤其是对于动态加载的页面。不过,我相信 WinHTTPRequest.5.1
有一个您可以使用的响应状态。
在这种情况下,您可能需要使用其他方法,例如基于 IE 浏览器并等待页面加载。您仍然会使用 HTMLDocument
的 .querySelector
或 .querySelectorAll
方法。
示例脚本:
- 此处
IE
的方法:https://whosebug.com/a/50613063/6241235 WinHTTP
的方法:https://whosebug.com/a/50759945/6241235 OR https://whosebug.com/a/50757822/6241235
信息: