如何使用xpath获取ul中所有图像的src?

我需要找到所有属于这个 ul 图像的 src,为此,我正在使用 xpath 来完成这项工作,但是它只返回第一个映像的 src。该如何获得所有 src? 下面是我的 HTML 结构。

<ul id="product-slider">
  <li >
    <img src="1.jpg" width="310" height="363" />
  </li>
  <li>
    <img src="2.jpg" style="opacity: 1;">
 </li>
 <li>
    <img  width="310" height="363" src="3.jpg">
 </li>      
</ul>

下面是我的代码片段:

$img = $xpath->query("//ul[@id='product-slider']//img/@src");
foreach($img as $i){
  echo $i->nodeValue;
}

它找不到所有图像的 src,除了第一个。有人能提供一些帮助吗?

看看这个http://codepad.viper-7.com/RiiFfK

donglei2288
donglei2288 而不是$i->nodeValue;使用$i->getAttribute('src');
5 年多之前 回复

1个回答

To get a list of all images (actually, all the values from the src attributes) inside this ul, in your case, you can use XPath Axes:

//ul[@id='product-slider']/descendant::img/@src

What this does is:

  1. /ul[@id='product-slider'] selects the ul element with id='product-seller'

  2. /descendant::img selects all the descendants (children, grandchildren, etc.) from the ul

  3. /@src selects the src attribute of each one of these descendants.

XPath Axes is a great tool, you can find more information about it in here: http://www.w3schools.com/xpath/xpath_axes.asp (I know they're not the most relliable source for everything, but this XPath tutorial is quite a good intro).

Edit: Using OP's new code snippet from codepad as an example: The captured element inside the source code from Snapdeal Link is the following ul, in which only the first img tag contains a src attribute.

    <ul id="product-slider" class="mainImageSlider">
        <li>
            <img title="Apple iPhone 5S 16 GB (Gold)"  class="jqzoom zoomPad lazyBg" itemprop="image"  src="http://n2.sdlcdn.com/imgs/a/j/x/large/Apple-iPhone-5S-16-GB-SDL218153659-1-d5617.jpg" width="310" height="363" alt="Apple iPhone 5S 16 GB (Gold)" bigimage="http://n4.sdlcdn.com/imgs/a/j/x/Apple-iPhone-5S-16-GB-SDL218153659-1-d5617.jpg">
        </li>
        <li>
            <img title="Apple iPhone 5S 16 GB (Gold)"  class="jqzoom zoomPad lazyBg"  lazySrc="http://n1.sdlcdn.com/imgs/a/j/x/large/Apple-iPhone-5S-16-GB-SDL218153659-2-af89e.jpg" width="310" height="363" alt="Apple iPhone 5S 16 GB (Gold)" bigimage="http://n1.sdlcdn.com/imgs/a/j/x/Apple-iPhone-5S-16-GB-SDL218153659-2-af89e.jpg">
        </li>
        <li>
            <img title="Apple iPhone 5S 16 GB (Gold)"  class="jqzoom zoomPad lazyBg"  lazySrc="http://n4.sdlcdn.com/imgs/a/p/0/large/Apple-iPhone-5S-16-GB-SDL218153659-3-14019.jpg" width="310" height="363" alt="Apple iPhone 5S 16 GB (Gold)" bigimage="http://n4.sdlcdn.com/imgs/a/p/0/Apple-iPhone-5S-16-GB-SDL218153659-3-14019.jpg">
        </li>
        <li>
            <img title="Apple iPhone 5S 16 GB (Gold)"  class="jqzoom zoomPad lazyBg"  lazySrc="http://n4.sdlcdn.com/imgs/a/p/0/large/Apple-iPhone-5S-16-GB-SDL218153659-4-fa8f3.jpg" width="310" height="363" alt="Apple iPhone 5S 16 GB (Gold)" bigimage="http://n4.sdlcdn.com/imgs/a/p/0/Apple-iPhone-5S-16-GB-SDL218153659-4-fa8f3.jpg">
        </li>
    </ul>

</div>
dousi6192
dousi6192 它没有。 我刚刚更新了答案,请检查。 顺便说一句,如果你使用id而不是类来匹配XPath表达式会更好:它们的唯一性更可靠。
5 年多之前 回复
douxiong2738
douxiong2738 所有四个<img />标签都包含src属性@bruno
5 年多之前 回复
douwei1950
douwei1950 例如,你将得到4个网址。
5 年多之前 回复
dtqqq24248
dtqqq24248 谢谢你的上述解释。 但是它仍然只返回一个图像的src。 @bruno请看一下这个链接
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问