使用xpath提取属性值

时间:2014-02-12 17:30:06

标签: html xpath

我需要从此HTML中提取属性src的值。

例如,来自此HTML

<div class="css_telo js_telo">
<div class="css_teloV">
<div class="css_vlavo">
   <div class="css_info css_zena">
      <a class="css_foto big_foto" href="" title="Fotka: endoskopia"><span><img id="PPAsrc" src="http://m3.aimg.sk/profil/v_08c261dabb82e3eaded3e54c28c49e35.jpg?13922246241392224624" width="176" height="220" alt="Fotka: endoskopia" /></span></a><a href="/sluzby/fotka/profilova/?i9=8d6d0b1a0798" class="c_zmenfoto" title="Kliknutím si môžeš zmeniť fotku">Zmeniť fotku</a>
   </div>
   <div class="por_0">
      <div class="css_album css_menubox edit c_prazdne" id="m_fotoalbumy">
         <h3>Fotoalbumy<small> (0)</small></h3>
      </div>
   </div>
   <div class="por_1">
      <div class="css_album c_video xcss_akt css_menubox edit" id="m_videoalbumy">
         <h3>Videoalbumy<small> (0)</small></h3>
      </div>
   </div>
   <div class="por_2">
      <div class="css_priatelia css_menubox edit" id="m_userFriends">
         <h3>Priatelia<small> (0)</small></h3>
      </div>
   </div>
</div>

我需要这个:

http://m3.aimg.sk/profil/v_08c261dabb82e3eaded3e54c28c49e35.jpg?13922246241392224624

我尝试了这个XPATH表达式:

//div[@class="css_telo js_telo"]/div[@class="css_teloV"]/div[@class="css_vlavo"]/div[@class="css_info css_zena"]/a/span/img/@src

但它不起作用。哪里有问题?

1 个答案:

答案 0 :(得分:1)

它看起来像是页面上唯一的src,你可以简单地做到:

//img/@src

或更具体:

//img[@id="PPAsrc"]/@src