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

标签 html xml xpath google-sheets google-sheets-importxml

长期读者,第一次海报。

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

试图从 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”中。

模式一:

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

enter image description here

模式二:

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

enter image description here

模式三:

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

enter image description here

模式四:

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

enter image description here

模式5:

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

enter image description here

注意事项:

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

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

引用:

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

关于html - 如何在 Google 表格中使用 IMPORTXML 和 Xpath 获取 <img src> URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58352158/

相关文章:

javascript - 使用 node.js 渲染 html

html - 是否可以使用 CSS 像这样重叠堆叠无序列表?

xml - Xpath:紧跟在元素后面的第一个字母/字符串

javascript - 在 Safari 中上传图片不起作用

javascript - 如何知道所选文本是否在特定的 div 内

c++ - Xerces-c SaxParser 问题

sql-server - 如何将SQL查询结果保存到磁盘上的XML文件

java - @Qualifier() 和 @Autowired() 不起作用

xml - XSLT 总结值

c# - 如何使XPath仅在每个表中搜索?