xpath轴

复制代码
child::book    选取所有属于当前节点的子元素的 book 节点。
attribute::lang    选取当前节点的 lang 属性。
child::*    选取当前节点的所有子元素。
attribute::*    选取当前节点的所有属性。
child::text()    选取当前节点的所有文本子节点。
child::node()    选取当前节点的所有子节点。
descendant::book    选取当前节点的所有 book 后代。
ancestor::book    选择当前节点的所有 book 先辈。
ancestor-or-self::book    选取当前节点的所有 book 先辈以及当前节点(如果此节点是 book 节点)
child::*/child::price    选取当前节点的所有 price 孙节点。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from lxml import etree
 
html = '''
<!DOCTYPE html>
<html lang="en">
<head>
    <!--网页头部信息-->
    <title>网页名</title>
</head>
<body>
    <!--下面是网页正文-->
    <div class="two" name='test' style="color:red">id-text</div>
    <div class="one two">class-text</div>
    <div class="one">class-span</div>
    <div class="three">three</div>
</body>
</html>
'''
 
html =etree.HTML(html)
 
content1 = html.xpath("//div/ancestor::*") #选取div的所有父和祖父节点
content1 = html.xpath("//div[@class='two']/attribute::*") #选取节点的所有属性
content1 = html.xpath("//div[@class='two']/attribute::style") #选取节点的style属性
content1 = html.xpath(".//body/child::div") #选取所有子节点
 
print(content1)

  

 

posted @ 2020-03-11 13:56  brady-wang  阅读(230)  评论(0)    收藏  举报
编辑推荐:
· 编码之道,道心破碎。
· 记一次 .NET 某发证机系统 崩溃分析
· 微服务架构学习与思考:SOA架构与微服务架构对比分析
· tomcat为什么假死了
· 聊一聊 Linux 上对函数进行 hook 的两种方式
阅读排行:
· 编码之道,道心破碎。
· 知名开源项目Alist被收购!惹程序员众怒,开团炮轰甲方
· 如何给 GitHub Copilot "洗脑”,让 AI 精准遵循指令产出高质量代码
· 突发,小红书开发者后门被破解?!
· 历时半年,我将一个大型asp.net的零代码快速开发平台转成了java
历史上的今天:
2019-03-11 MySQL的SELECT ...for update
2016-03-11 点击图片实现文件上传
2016-03-11 预览上传
2016-03-11 使用 Vagrant 打造跨平台开发环境fffff
点击右上角即可分享
微信分享提示