如何在 Google 表格中使用 IMPORTXML 和 Xpath 获取 <img src> URL?

the*_*sea 4 html xpath google-sheets web-scraping google-sheets-formula

尝试使用 Google Sheets 功能从网页中获取图像的 URL 或 URL 片段IMPORTXML。我相当确定我的 Xpath 是正确的,但我要么什么也没得到,要么是“无法解析数据”——但我在这里看到了人们以这种方式抓取 Google 表格中 URL 的其他示例,尽管稍微有点不同的情况,似乎都不适用于此。

尝试从此页面抓取- 这是相关的 HTML:

<div id="product_image" class="A_ProductImg">
        <div class="bx-wrapper" style="max-width: 100%;"><div class="bx-viewport" style="width: 100%; overflow: hidden; position: relative; height: 540px;"><ul class="A_ProductImgSlider" style="width: auto; position: relative;">
         <li class="A_ItemList" style="float: none; list-style: none; position: absolute; width: 540px; z-index: 50; display: block;">
          <div class="image A_ItemImg A_SquareOuter">
           <img src="/ec/img/D3-64I011012_M_s.jpg" onerror="this.src='/ec/images/common/NoImage.gif'" alt="main product image" id="mainImage" class="A_ItemProductImg A_Square">
          </div>
         </li>
         <li class="A_ItemList" style="float: none; list-style: none; position: absolute; width: 540px; z-index: 0; display: none;"><div class="image A_ItemImg A_SquareOuter"><img src="/ec/img/D3-64I011012_S_1m.jpg" alt="product image 1" class="A_ItemProductImg A_Square"></div></li>
         <li class="A_ItemList" style="float: none; list-style: none; position: absolute; width: 540px; z-index: 0; display: none;"><div class="image A_ItemImg A_SquareOuter"><img src="/ec/img/D3-64I011012_S_2m.jpg" alt="product image 2" class="A_ItemProductImg 
Run Code Online (Sandbox Code Playgroud)

我进行了以下查询来尝试使用后续的 HTML:

=IMPORTXML(A2,"//*[@id='product_image']/div[1]/div[1]/ul/li[1]/div/img src")
Run Code Online (Sandbox Code Playgroud)

A2 具有相关的 URL。

我认为 Xpath 在那里是正确的,但不确定为什么它不会给我我正在寻找的结果。我玩了一下,但没有运气。

Tan*_*ike 5

这个答案怎么样?请将此视为多个答案之一。

公式示例:

在本例中,https://www.mikigakki.com/ec/pro/disp/H/D3-64I011012?sFlg=2被放入单元格“A1”中。

模式一:

=IMPORTXML(A1,"//img/@src")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

模式2:

=IMPORTXML(A1,"//li//@src")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

模式3:

=IMPORTXML(A1,"//li[position()>1]//@src")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

模式4:

=ARRAYFORMULA("https://www.mikigakki.com"&IMPORTXML(A1,"//li[position()>1]//@src"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

模式5:

=ARRAYFORMULA("https://www.mikigakki.com"&IMPORTXML(A1,"//li[1]//@src"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

笔记:

  • 如果您想使用 检索第一个图像 url *[@id='product_image'],您还可以使用以下公式。

    =IMPORTXML(A1,"//*[@id='product_image']/ul/li[1]/div/img/@src")
    
    Run Code Online (Sandbox Code Playgroud)

参考:

如果我误解了你的问题并且这不是你想要的结果,我深表歉意。