zoukankan      html  css  js  c++  java
  • xpath教程二 ---- 通过ID和Class检索

    必备知识点

    • 在html中,id是唯一的
    • 在html中,class是可以多处引用的

    工具

    • Python3版本
    • lxml库【优点是解析快】
    • HTML代码块【从网络中获取或者自己杜撰一个】
    • requests【推荐安装,从网页上获取网页代码练手,再好不过了】

    Xpath学习

    先定义html代码块【这次只从body开始】

    <body>
    <div class="container">
        <div id="first">
            <div class="one">都市</div>
            <div class="two">德玛西亚</div>
            <div class="two">王牌对王牌</div>
            <a>
                <div class="spe">特殊位置</div>
            </a>
        </div>
        <div id="second">
            <div class="three">水电费</div>
            <div class="three">说的话房间不开封</div>
            <div class="four">三顿饭黑客技术</div>
        </div>
        <div id="third">
            <div class="three">水电费</div>
            <div class="three">说的话房间开封</div>
        </div>
    </div>
    </body>
    """
    

    再准备python代码块

    from lxml import etree
    
    html = etree.HTML(html_str)
    

    任务一:获取类名为one的文本值

    解决这个问题,有非常简单的xpath路径,直接匹配html代码中的class,然后获取文本值就行

    代码如下:

    print(html.xpath('.//div[@class="one"]/text()'))
    

    结果:['都市']

    这里需要解释多个地方: - @的作用:表示属性,div属于标签,它有自己的属性,例如classid等等。 - 点 . 的作用:表示当前位置;与其对应的是双点 .. :表示上一层级的位置 - 双斜杠 // 的作用:查找当前标签下所有子级中搜索;与其对应的是单斜杆 / ,这个标签标签下一层所有中搜索。【后面两个任务是这点的练习】

    任务二:获取id为first下,第一层子级div标签的文本值

    只需要获取第一层,使用单斜杆就足够了,xpath路径如下:

    print(html.xpath('.//div[@id="first"]/div/text()'))
    

    结果:['都市', '德玛西亚', '王牌对王牌']

    任务三:获取id为first下,所有层级div标签的文本值

    这个任务和上一个任务形成对比,一个是单斜杆一个是双斜杠,则xpath的代码如下:

    print(html.xpath('.//div[@id="first"]//div/text()'))
    

    结果:['都市', '德玛西亚', '王牌对王牌', '特殊位置']

    任务四:获取id为second下,所有类为threediv标签的文本值

    指定id为second,并且子级div的类名是three,然后是获取文本,则xpath如下

    print(html.xpath('.//div[@id="second"]/div[@class="three"]/text()'))
    

    结果:['水电费', '说的话房间不开封']

    任务五:获取所有类为threediv标签的文本值

    观察html代码块,会发现类为threediv标签在几个地方,所以这里最好的方法就是全局范围内的直接搜索,简单粗暴的xpath如下:

    print(html.xpath('.//div[@class="three"]/text()'))
    

    结果:['水电费', '说的话房间不开封', '水电费', '说的话房间开封']

    任务六:获取文本等于水电费的标签,取出他们的class

    通过文本值,获取他们的类名信息,就是把上一个任务反过来做就行,xpath如下:

    print(html.xpath('.//div[text()="水电费"]/@class'))
    

    结果:['three', 'three']

    最终的代码和运行截图

    html_str = """
    <body>
    <div class="container">
        <div id="first">
            <div class="one">都市</div>
            <div class="two">德玛西亚</div>
            <div class="two">王牌对王牌</div>
            <a>
                <div class="spe">特殊位置</div>
            </a>
        </div>
        <div id="second">
            <div class="three">水电费</div>
            <div class="three">说的话房间不开封</div>
            <div class="four">三顿饭黑客技术</div>
        </div>
        <div id="third">
            <div class="three">水电费</div>
            <div class="three">说的话房间开封</div>
        </div>
    </div>
    </body>
    """
    
    from lxml import etree
    
    html = etree.HTML(html_str)
    print(html.xpath('.//div[@class="one"]/text()'))
    print(html.xpath('.//div[@id="first"]/div/text()'))
    print(html.xpath('.//div[@id="first"]//div/text()'))
    print(html.xpath('.//div[@id="second"]/div[@class="three"]/text()'))
    print(html.xpath('.//div[@class="three"]/text()'))
    print(html.xpath('.//div[text()="水电费"]/@class'))
    
    

    xpath_2

    版权声明:允许转载,转载请注明出处 —— 《xpath教程》: 通过ID和Class检索

  • 相关阅读:
    《结对-航空购票系统-测试过程》
    《结对-航空购票系统-开发过程》
    课后作业-阅读任务-阅读提问-2
    《1005-构建之法:现代软件工程-阅读笔记》
    《团队-记事本程序-代码设计规范》
    Python作业:jieba库
    Python第四周作业:青蛙跳台阶、欧式距离、验证码校验、大小写互换、凯撒加密、身份证号处理
    Python汉诺塔问题
    第四周(1):利用Python计算π的值,并显示进度条
    Python第二周(1):凯撒密码B,括号配对测试,字符串反码,计算矩形面积,快乐的数字
  • 原文地址:https://www.cnblogs.com/brady-wang/p/8946094.html
Copyright © 2011-2022 走看看