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

问题描述 投票:0回答:1

目前,我们使用下面提到的代码进行数据提取,但是代码没有从网页中提取完整的数据,代码忽略了当我在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>
html vba web-scraping ms-access-2013
1个回答
0
投票

该内容需要在页面上运行javascript,因此您需要:

  1. 在网络流量中搜索这些网址的那些部分的信息,看看你是否从其他地方获得(你可以 - 如下所示);要么,
  2. 自动化浏览器,例如与微软互联网控制

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

 <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)网络选项卡 - 不同的网址

使用网络选项卡中找到的不同URL返回包含链接的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
© www.soinside.com 2019 - 2024. All rights reserved.