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
  • 相关阅读:
    python第三十一课--递归(1.简单递归函数的定义和使用)
    python第三十课--异常(with as操作)
    python第三十课--异常(异常对象传递过程)
    python第三十课--异常(raise关键字)
    python第三十课--异常(else讲解)
    python第三十课--异常(finally讲解)
    python第三十课--异常(异常处理定义格式和常见类型)
    python第二十九课——文件读写(复制文件)
    python第二十九课——文件读写(写数据的操作)
    16 nginx实现负载均衡
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349013.html
Copyright © 2011-2022 走看看