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

How to grab an <img src> URL using IMPORTXML and Xpath in Google Sheets?

好久reader,第一次发贴。

尝试使用 Google Sheets IMPORTXML 函数从网页中获取 URL 或 URL 图像片段。我相当确定我的 Xpath 是正确的,但我要么什么也得不到,要么得到一个 "that data can't be parsed" - 但我在这里看到了其他例子,人们在 Google Sheets this 中抓取 URLs方式,虽然在略有不同的情况下,其中 none 似乎适用于此。

正在尝试从 this page 抓取 - 这是相关的 HTML:

        <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 

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

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

A2 具有相关 URL。

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

非常感谢您花时间阅读本文并提供帮助。

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

示例公式:

在这种情况下,https://www.mikigakki.com/ec/pro/disp/H/D3-64I011012?sFlg=2 被放入单元格 "A1"。

模式 1:

=IMPORTXML(A1,"//img/@src")

模式 2:

=IMPORTXML(A1,"//li//@src")

模式 3:

=IMPORTXML(A1,"//li[position()>1]//@src")

模式 4:

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

模式 5:

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

注:

  • 如果要使用*[@id='product_image']检索第一张图像url,也可以使用以下公式。

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

参考:

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