美文网首页
_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 精准使用规则

    使用text()来做标记,用来确定位置. 测试文本 我们为了获取,第一个文本 需要在此基础上我们获取它的父节点下面...

  • Xpath使用规则

    引用维基百科 一,概述 XPath 是XML路径语言(xml path language),是一种用来确定XML文...

  • PMD - 用 XPath 开发代码规则

    在 PMD 的使用过程中,可以用 Java 或 XPath 来开发代码规则。 本文讲述如何使用 XPath 来开发...

  • XPath

    Xpath常用规则

  • 4.xml中Xpath的使用

    1.Xpath使用的规则 2. Xpath的使用,通过id来查找一个对象 3.给xml文件中插入一个对象的生成xml文档

  • Scrapy Shell辅助工具IPython

    工具名称IPython 使用·scrapy·时经常需要进入·scrapy·终端使用·xpath·测试规则是否能获取...

  • python爬虫之xpath

    一. python使用xpath 使用时先安装 lxml 包 二. xpath简介 XPath,全称 XML Pa...

  • PMD - 用 Java 开发代码规则

    在 PMD 的使用过程中,可以用 Java 或 XPath 来开发代码规则。 本文讲述如何使用 Java 来开发。...

  • lxml结合xpath注意事项

    1.使用Xpath语法,应该使用Element.xpath方法,来执行xpath选择 示例代码如下: 2.获取某个...

  • python爬虫之XPath解析

    XPath 简介: XPath 是一门在 XML 文档中查找信息的语言 什么是 XPath? XPath 使用路径...

网友评论

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

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