美文网首页
_xpath 精准使用规则

_xpath 精准使用规则

作者: VictorChi | 来源:发表于2018-06-08 11:39 被阅读0次
    使用text()来做标记,用来确定位置.

    测试文本

        <tbody>
        <tr class="result1">
            <th class="field-name">Type</th>
            <td>Electronic Thesis or Dissertation</td>
        </tr>
        <tr class="result2">
            <th class="field-name">Type</th>
            <td>Text</td>
        </tr>
        <tr class="result1">
            <th class="field-name">Type</th>
            <td>Image</td>
        </tr>
        <tr class="result2">
            <th class="field-name">Type</th>
            <td>StillImage</td>
        </tr>
        <tr class="result1">
            <th class="field-name">Language</th>
            <td>fr</td>
        </tr>
        <tr class="result2">
            <th class="field-name">Identifier</th>
            <td>
                <a onclick="ga('send', 'event', 'External-link', 'Identifier', '/full.php?id=1183922'); return logDownload('1183922');"
                   href="http://www.theses.fr/2016SACLS038"
                   title="View original record">http://www.theses.fr/2016SACLS038</a></td>
        </tr>
        </tbody>
    </table>
    
    测试1
    //th[.='Type'] # 获取到所有文本为Type的值.
    

    我们为了获取,第一个文本


    文本1

    需要在此基础上我们获取它的父节点下面的td的文本内容../td/text(),我们只需要获取第一个值加一个坐标.

    (//th[.='Type']/../td/text())[1]  # 得到预期的结果 Electronic Thesis or Dissertation
    
    使用属性的多值匹配 使用contains

    倘若属性的值发生变化.但是存在一定规律,如下图class='result1'或者是class='result2'之类的.我们需要获取他们的内容.

    <tr class="result1">
        <th class="field-name">Type</th>
        <td>Electronic Thesis or Dissertation</td>
    </tr>
    <tr class="result2">
        <th class="field-name">Type</th>
        <td>Text</td>
    </tr>
    <tr class="result1">
        <th class="field-name">Type</th>
        <td>Image</td>
    </tr>
    <tr class="result2">
        <th class="field-name">Type</th>
        <td>StillImage</td>
    </tr>
    <tr class="result1">
        <th class="field-name">Language</th>
        <td>fr</td>
    </tr>
    

    xpath 语法

    //tr[contains(@class,'result')] # 得到所有class 包含result的语句
    
    获取多个参数
    <div class="accordion-tabbed__tab-mobile ">
        <a href="#" data-id="a2" data-db-target-for="a2" title="Costa M. L."
           class="author-name accordion-tabbed__control visible-x"><span>Costa M. L.</span><i aria-hidden="true"
                                                                                              class="icon-arrow_d_n"></i></a>
        <div data-db-target-of="a2" class="author-info accordion-tabbed__content"><p>PhD, FRCS (Tr &amp; Orth), Clinical
            Senior Lecturer</p>
            <p class="author-type"></p>
            <p></p>
            <p>1Clinical Sciences Institute University of Warwick Medical School, Clinical Sciences Building, University
                Hospital, Clifford Bridge Road, Coventry CV2 2DX, UK.</p>
            <div class="bottom-info"><p><a href="/author/Costa%2C+M+L">
                Search for more papers by this author
            </a></p></div>
        </div>
    </div>
    
    demo
    需要一条xpath获取他们的名字,职位,跟机构.
    //div[a/span/text() and div/p/text() and div/div/p/a/text()]
    

    相关文章

      网友评论

          本文标题:_xpath 精准使用规则

          本文链接:https://www.haomeiwen.com/subject/gleosftx.html