zoukankan      html  css  js  c++  java
  • xpath /和//

    / 和//区别:
    
    1. 使用1个/
    
    <html>
    <li>aaa</li>
    <li>bbb</li>
    <ul>
             <li class="item-0">a01<a href="link1.html">first item</a></li>
             <li class="item-1">b02<a href="link2.html">second item</a></li>
             <li class="item-inactive">c03<a href="link3.html">third item</a></li>
             <li class="item-1">d04<a href="link4.html">fourth item</a></li>
             <li class="item-0">e05<a href="link5.html">fifth item</a></li>
        </ul>
    </html>
    
    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    from lxml import etree
    
    # 获取文件元素
    from lxml import etree
    
    # 获取文件元素
    htmlEmt = etree.parse('test01.html')
    # 获取所有的 <li> 标签
    result = htmlEmt.xpath('/html/li')
    print(result)
    print type(result)
    for x in result:
        print x
        print type(x)
        print '-------------------------'
        print x.text
    	
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/xpath/l1.py
    [<Element li at 0x268a2d8>, <Element li at 0x268a9b8>]
    <type 'list'>
    <Element li at 0x268a2d8>
    <type 'lxml.etree._Element'>
    -------------------------
    aaa
    <Element li at 0x268a9b8>
    <type 'lxml.etree._Element'>
    -------------------------
    bbb
    
    Process finished with exit code 0
    
    
    
    2.
    改成//后
    
    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    from lxml import etree
    
    # 获取文件元素
    from lxml import etree
    
    # 获取文件元素
    htmlEmt = etree.parse('test01.html')
    # 获取所有的 <li> 标签
    result = htmlEmt.xpath('//li')
    print(result)
    print type(result)
    for x in result:
        print x
        print type(x)
        print '-------------------------'
        print x.text
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/xpath/l1.py
    [<Element li at 0x264a2d8>, <Element li at 0x264a9b8>, <Element li at 0x264a170>, <Element li at 0x264a0a8>, <Element li at 0x264a210>, <Element li at 0x264a418>, <Element li at 0x264a4b8>]
    <type 'list'>
    <Element li at 0x264a2d8>
    <type 'lxml.etree._Element'>
    -------------------------
    aaa
    <Element li at 0x264a9b8>
    <type 'lxml.etree._Element'>
    -------------------------
    bbb
    <Element li at 0x264a170>
    <type 'lxml.etree._Element'>
    -------------------------
    a01
    <Element li at 0x264a0a8>
    <type 'lxml.etree._Element'>
    -------------------------
    b02
    <Element li at 0x264a210>
    <type 'lxml.etree._Element'>
    -------------------------
    c03
    <Element li at 0x264a418>
    <type 'lxml.etree._Element'>
    -------------------------
    d04
    <Element li at 0x264a4b8>
    <type 'lxml.etree._Element'>
    -------------------------
    e05
    
    Process finished with exit code 0
  • 相关阅读:
    ShellExecuteEX打开iqy文件导致excel hang的原因分析
    3种方法遍历商人访问5个城市问题
    用CRT查找内存泄漏
    同步异步和阻塞5-异步非阻塞
    同步异步和阻塞4-同步非阻塞
    同步异步和阻塞3-同步阻塞
    同步异步和阻塞2-测试小项目
    同步异步和阻塞1
    IDA分析脱壳后丢失导入表的PE
    计算机中补码的数学运算逻辑及证明
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349013.html
Copyright © 2011-2022 走看看