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
  • 相关阅读:
    Win64 驱动内核编程-12.回调监控进线程创建和退出
    我的主站 SHARELIST -分享列表 (功能持续完善中 2019-11-24 版本0.3)
    Caddy-基于go的微型serve用来做反向代理和Gateway
    Docker Swarm删除节点
    Docker Swarm集群搭建
    Docker Machine-Windows
    Docker Compose
    Docker网络配置进阶
    Docker网络相关
    Docker数据管理
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349013.html
Copyright © 2011-2022 走看看