在访问VBA中使用MSXML2.XMLHTTP不能提取所有页面数据

时间:2019-04-28 08:29:38

标签: html vba web-scraping ms-access-2013

当前,我们使用下面提到的代码进行数据提取,但是代码未从网页中提取完整的数据,代码忽略了当我在Internet Explorer上启用Java脚本和DOM存储时可见的数据。

到目前为止,我使用下面提到的代码,后缀代码是提取网页中接受图像的所有内容。

我的代码受到打击。

Set http = CreateObject("MSXML2.XMLHTTP")

    http.Send
    html.body.innerHTML = http.ResponseText

    On Error GoTo 0
    html1 = html.body.innerHTML
     brand5 = html.documentElement.innerHTML
     If html1 Like "*media__thumb*" Then
other_img = html.getElementsByClassName("media__thumb")(0).innerText
'other_img = other_img.innerHTML
End If

在网页上,下面给出了多个图片html代码(请注意,我上面的代码不是从下面提到的html代码中提取数据。


            <a class="media__thumbnail" data-media_type="IMAGE" data-media_id="orbit-bagged-53017-64" data-target="IMAGE" data-has-index="true">
                <img src="https://images.yourweb/_145.jpg">
            </a>
            <a class="media__thumbnail media__thumbnail--selected" data-media_type="IMAGE" data-media_id="orbit-bagged-53017-e1" data-target="IMAGE" data-has-index="true">
                <img src="https://images.yourweb1_145.jpg">
            </a>
            </span></a>

http.response在下面给出

<div id="thumbnails" class="media__thumbnails" data-component="thumbnails"></div>

    <script type="text/template" id="media__thumbnails">
        {{#thumbnails}}
            <a class="media__thumbnail" data-media_type="{{type}}" data-media_id="{{id}}" data-target="{{type}}" data-has-index="true">
                <img src="{{{thumb}}}"/>
                {{# hasIcon}}
                  {{# threeSixtyIcon}} <div class="whitespace"><span class="threesixtyIcon"></span></div>{{/ threeSixtyIcon}}
                  {{^ threeSixtyIcon}} <span class="videoIcon"></span>{{/ threeSixtyIcon}}
                {{/ hasIcon}}
            </a>
        {{/thumbnails}}
        {{#additionalThumbnailsThumbnail}}
            <a class="media__thumbnail media__thumbnail-additional-count" data-media_type="{{type}}" data-media_id="{{id}}" data-target="{{type}}" data-has-index="true">
                <img src="{{{thumb}}}"/>
                {{# hasIcon}}
                  {{# threeSixtyIcon}} <div class="whitespace"><span class="threesixtyIcon"></span></div>{{/ threeSixtyIcon}}
                  {{^ threeSixtyIcon}} <span class="videoIcon"></span>{{/ threeSixtyIcon}}
                {{/ hasIcon}}
                {{#additionalImagesCount}}
                    <div class="media__thumbnail-overlay"></div>
                    <span class="media__thumbnail-count">+{{additionalImagesCount}}</span>
                {{/additionalImagesCount}}
            </a>

1 个答案:

答案 0 :(得分:0)

该内容需要在页面上运行javascript,因此您需要执行以下操作之一

  1. 在网络流量中搜索那些url的那部分信息,以查看是否从其他地方获取(您可以-如下所示);或者,
  2. 自动化浏览器,例如使用Microsoft Internet控件

您可以从以下内容看到内容是动态加载的:

 <script type="text/template" id="media__thumbnails">
        {{#thumbnails}}
            <a class="media__thumbnail" data-media_type="{{type}}" data-media_id="{{id}}" data-target="{{type}}" data-has-index="true">
                <img src="{{{thumb}}}"/>
                {{# hasIcon}}
                  {{# threeSixtyIcon}} <div class="whitespace"><span class="threesixtyIcon"></span></div>{{/ threeSixtyIcon}}
                  {{^ threeSixtyIcon}} <span class="videoIcon"></span>{{/ threeSixtyIcon}}
                {{/ hasIcon}}
            </a>
        {{/thumbnails}}
        {{#additionalThumbnailsThumbnail}}
        ...... 
        {{/additionalThumbnails}}
    </script>
    <script type="text/template" 


1)网络标签-不同的网址

使用在网络标签中找到的其他网址,返回包含链接的json。响应为json,因此需要jsonparser

'VBE>Tools> References> Add reference to Microsoft Scripting Runtime
'Download and add in jsonconverter.bas from https://github.com/VBA-tools/VBA-JSON/blob/master/JsonConverter.bas

将converter.bas代码放在module2中,并注释掉以下行:Attribute VB_Name = "JsonConverter"

在module1中放置GetInfo子项。

Option Compare Database
Option Explicit
Public Sub GetInfo()
    Dim json As Object, url1 As String, url2 As String, url3 As String

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://www.homedepot.com/p/svcs/frontEndModel/100001020?_=1556447908065", False
        .send
        Set json = Module2.ParseJson(.responseText)
    End With
    'Parse json object (see paths shown below for example)
     url1 = json("primaryItemData")("media")("mediaList")(2)("location")
     url2 = json("primaryItemData")("media")("mediaList")(3)("location")
     url3 = json("primaryItemData")("media")("mediaList")(4)("location")   'example
    Stop '<==delete me later
End Sub

前3个缩略图的路径:

json►primaryItemData►media►mediaList►2►location
json►primaryItemData►media►mediaList►3►location
json►primaryItemData►media►mediaList►4►location

浏览json here


2)自动化浏览器(IE版本):

'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub GetImageLinks()
    Dim ie As New InternetExplorer, images As Object, i As Long
    With ie
        .Visible = True
        .Navigate2 "https://www.homedepot.com/p/Orbit-Sandstone-Rock-Valve-Box-Cover-53017/100001020"

        While .Busy Or .readyState < 4: DoEvents: Wend
        Set images = .document.querySelectorAll(".media__thumbnail img")

        For i = 0 To images.Length - 1
            Debug.Print images.item(i).src
        Next

        Stop
        .Quit
    End With
End Sub