lxml基础
节点操作:
from lxml import etree # 1.创建Element对象,参数即节点名称
root = etree.Element('root')
print(root) # <Element root at 0x1551e08> # 2.获取节点名称,tag
print(root.tag) # root # 3.输出XML内容,tostring,参数为Element对象
print(etree.tostring(root)) # b'<root/>' # 4.添加子节点,SubElement,第一个参数为父节点(Element对象),第二个参数为子节点名称
child1 = etree.SubElement(root, 'child1')
child2 = etree.SubElement(root, 'child2')
child3 = etree.SubElement(root, 'child3')
print(etree.tostring(root)) #b'<root><child1/><child2/><child3/></root>' # 5.删除子节点,remove删除指定节点,参数为Element对象。clear清空所有节点。
# root.remove(child1) # 删除指定子节点
# print(etree.tostring(root))
# root.clear() # 清除所有子节点
# print(etree.tostring(root)) # b'<root/>' # 6.以列表的方式操作子节点
# 下标访问
child = root[0]
print(child.tag) #child1 # 子节点数量
print(len(root)) # 3 # 获取索引号
print(root.index(child2)) #1 # 遍历
for child in root:
print(child.tag)
# child1
# child2
# child3 # 插入
root.insert(0, etree.Element('child0'))
print(etree.tostring(root)) #b'<root><child0/><child1/><child2/><child3/></root>' # 切片,切出来的还是列表
start = root[:1]
end = root[-1:]
print(start[0].tag, end[0].tag) # child0 child3 # 尾部添加
root.append( etree.Element('child4') )
print(etree.tostring(root)) # b'<root><child0/><child1/><child2/><child3/><child4/></root>' # 7.获取父节点
print(child1.getparent()) # <Element root at 0x14e1e08>
print(child1.getparent().tag) # root
属性操作:
# 属性是以key-value的方式存储的,就像字典一样。 # 1.创建属性
# 可以在创建Element对象时同步创建属性,第二个参数即为属性名和属性值:
root = etree.Element('root', interesting='totally')
print(etree.tostring(root)) # b'<root interesting="totally"/>'
# 也可以使用set方法给已有的Element对象添加属性,两个参数分别为属性名和属性值:
root.set('hello', 'Huhu')
print(etree.tostring(root)) # b'<root interesting="totally" hello="Huhu"/>' # 2.获取属性
# 属性是以key-value的方式存储的,就像字典一样。
# get,获得某一个属性值
print(root.get('interesting')) # totally # keys,获取所有的属性名
print(sorted(root.keys())) # ['hello', 'interesting'] # items,获取所有的键值对
# for name, value in root.items():
# print('%s = %r' % (name, value))
for name, value in sorted(root.items()): # sorted还可以排序
print('%s = %r' % (name, value))
# interesting = 'totally'
# hello = 'Huhu' # 也可以用attrib属性一次拿到所有的属性及属性值存于字典中: attributes = root.attrib
print(attributes) # {'hello': 'Huhu', 'interesting': 'totally'} attributes['good'] = 'Bye' # 字典的修改影响节点
print(root.get('good')) # Bye
文本操作:
# 1.text和tail属性
# 一般情况,可以用Element的text属性访问标签的文本。
root = etree.Element('root')
root.text = 'Hello, World!'
print(root.text) # Hello, World!
print(etree.tostring(root)) # b'<root>Hello, World!</root>' # XML的标签一般是成对出现的,有开有关,但像HTML则可能出现单一的标签,比如下面这段代码中的<br/>。
# <html><body>Text<br/>Tail</body></html> # Element类提供了tail属性支持单一标签的文本获取。
html = etree.Element('html')
body = etree.SubElement(html, 'body')
body.text = 'Text'
print(etree.tostring(html)) # b'<html><body>Text</body></html>' br = etree.SubElement(body, 'br')
print(etree.tostring(html)) # b'<html><body>Text<br/></body></html>' # tail仅在该标签后面追加文本
br.tail = 'Tail'
print(etree.tostring(br)) # b'<br/>Tail'
print(etree.tostring(html)) # b'<html><body>Text<br/>Tail</body></html>' # tostring方法增加method参数,过滤单一标签,输出全部文本
print(etree.tostring(html, method='text')) # b'TextTail' # 2.XPath方式
# 方式一:过滤单一标签,返回文本
print(html.xpath('string()')) # TextTail
# 方式二:返回列表,以单一标签为分隔
print(html.xpath('//text()')) # ['Text', 'Tail'] # 方法二获得的列表,每个元素都会带上它所属节点及文本类型信息,如下:
texts = html.xpath('//text()') print(texts[0]) # Text
# 所属节点
parent = texts[0].getparent()
print(parent.tag) # body print(texts[1], texts[1].getparent().tag) # Tail br # 文本类型:是普通文本还是tail文本
print(texts[0].is_text) # True
print(texts[1].is_text) # False
print(texts[1].is_tail) # True
lxml基础的更多相关文章
- 05.Python网络爬虫之三种数据解析方式
引入 回顾requests实现数据爬取的流程 指定url 基于requests模块发起请求 获取响应对象中的数据 进行持久化存储 其实,在上述流程中还需要较为重要的一步,就是在持久化存储之前需要进行指 ...
- 05,Python网络爬虫之三种数据解析方式
回顾requests实现数据爬取的流程 指定url 基于requests模块发起请求 获取响应对象中的数据 进行持久化存储 其实,在上述流程中还需要较为重要的一步,就是在持久化存储之前需要进行指定数据 ...
- 《Python网络爬虫之三种数据解析方式》
引入 回顾requests实现数据爬取的流程 指定url 基于requests模块发起请求 获取响应对象中的数据 进行持久化存储 其实,在上述流程中还需要较为重要的一步,就是在持久化存储之前需要进行指 ...
- python网络爬虫数据中的三种数据解析方式
一.正则解析 常用正则表达式回顾: 单字符: . : 除换行以外所有字符 [] :[aoe] [a-w] 匹配集合中任意一个字符 \d :数字 [0-9] \D : 非数字 \w :数字.字母.下划线 ...
- Scrapy笔记04- Selector详解
Scrapy笔记04- Selector详解 在你爬取网页的时候,最普遍的事情就是在页面源码中提取需要的数据,我们有几个库可以帮你完成这个任务: BeautifulSoup是python中一个非常流行 ...
- Python网络爬虫之三种数据解析方式 (xpath, 正则, bs4)
引入 回顾requests实现数据爬取的流程 指定url 基于requests模块发起请求 获取响应对象中的数据 进行持久化存储 其实,在上述流程中还需要较为重要的一步,就是在持久化存储之前需要进行指 ...
- python 3.x 爬虫基础---常用第三方库(requests,BeautifulSoup4,selenium,lxml )
python 3.x 爬虫基础 python 3.x 爬虫基础---http headers详解 python 3.x 爬虫基础---Urllib详解 python 3.x 爬虫基础---常用第三方库 ...
- Python爬虫基础之lxml
一.Python lxml的基本应用 <html> <head> <title> The Dormouse's story </title> </ ...
- Python爬虫基础——XPath语法的学习与lxml模块的使用
XPath与正则都是用于数据的提取,二者的区别是: 正则:功能相对强大,写起来相对复杂: XPath:语法简单,可以满足绝大部分的需求: 所以,如果你可以根据自己的需要进行选择. 一.首先,我们需要为 ...
随机推荐
- 微软BI 之SSAS 系列 - 多维数据集维度用法之二 事实维度(退化维度 Degenerate Dimension)
这篇文章是基于上一篇 SSAS 系列 - 多维数据集维度用法之一 引用维度 Referenced Dimension 继续讲解多维数据集维度用法中的事实维度. 事实维度,顾名思义就是把事实表 Fact ...
- 【ShoppingPeeker】-基于Webkit内核的爬虫蜘蛛引擎 ShoppingWebCrawler的姊妹篇-可视化任务Web管理
ShoppingPeeker 这个项目是蜘蛛项目的可视化任务站点. 项目github地址:ShoppingPeeker 开发语言:C# 开发工具:Visual Studio 2017 +.Net Co ...
- Linux下利用backtrace追踪函数调用堆栈以及定位段错误[转]
来源:Linux社区 作者:astrotycoon 一般察看函数运行时堆栈的方法是使用GDB(bt命令)之类的外部调试器,但是,有些时候为了分析程序的BUG,(主要针对长时间运行程序的分析),在程序 ...
- 浅析Sql Server参数化查询
说来惭愧,工作差不多4年了,直到前些日子被DBA找上门让我优化一个CPU占用很高的复杂SQL语句时,我才突然意识到了参数化查询的重要性. 相信有很多开发者和我一样对于参数化查询认识比较模糊,没有引起足 ...
- [Big Data - ZooKeeper] ZooKeeper: A Distributed Coordination Service for Distributed Applications
ZooKeeper ZooKeeper: A Distributed Coordination Service for Distributed Applications Design Goals Da ...
- sqlmap tamter
支持的数据库 编号 脚本名称 作用 实现方式 all 1 apostrophemask.py 用utf8代替引号 ("1 AND '1'='1") '1 AND %EF%BC%87 ...
- 【iCore4 双核心板_ARM】例程十七:USB_MSC实验——读/写U盘(大容量存储器)
实验方法: 1.将跳线冒跳至USB_UART,通过Micro USB 线将iCore4 USB-UART接口与电脑相连. 2.打开PUTTY软件. 3.通过读U盘转接线将U盘(或者读卡器)与iCore ...
- 【iCore1S 双核心板_ARM】例程十二:DMA实验——存储器到存储器的传输
实验原理: DAM(直接存储器访问)传输不需要占用CPU,可以在存储器至存储器实现高速的数据 传输.本实验采用DAM2控制器的数据流0,选用通道0进行数据传输.通过LED的颜色来 判断传输是否成功. ...
- java 中使用log4j
一.控制台使用 1.导入log4j包到工程中 2.配置: log4j.rootLogger=DEBUG,console,R log4j.appender.console=org.apache.log4 ...
- easyui 自定义验证规则 验证用户是否已经存在 远程ajax验证
easyui远程ajax验证 2014年09月30日 22:29:32 clj198606061111 阅读数:6130 标签: easyui 更多 个人分类: jqueryeasyui 版权声明:本 ...