lxml.etree.HTML(text) 解析HTML文档
0.参考
http://lxml.de/tutorial.html#the-xml-function
There is also a corresponding function HTML() for HTML literals.
>>> root = etree.HTML("<p>data</p>")
>>> etree.tostring(root)
b'<html><body><p>data</p></body></html>'
1.基本用法
from lxml import etree
# Parses an HTML document from a string constant. Returns the root nood
root = etree.HTML(r.text) #<Element html at 0x7bb8208>
1.1 xpath 和 cssselect 获取文字和属性
In [83]: for item in root.xpath('//button')[:1]:
...: print(item)
...: print(item.text) #获取文字
...: print(item.xpath('./@id'))
...:
<Element button at 0x84277c8>
Requests Generator
['btn_requests']
###
In [84]: for item in root.cssselect('button')[:1]:
...: print(item)
...: print(item.text)
...: print(item.cssselect('::attr(id)')) #不支持伪元素写法
...:
...:
<Element button at 0x84277c8>
Requests Generator
ExpressionError: Pseudo-elements are not supported.
###
In [92]: for item in root.cssselect('button')[:1]:
...: print(item.get('id', '')) #获取属性
btn_requests
###
In [93]: for item in root.cssselect('button')[:1]:
...: print(item.xpath('./@id')) #嵌套
...:
['btn_requests']
1.2 美化打印
print(etree.tostring(root, pretty_print=True).decode('utf-8')) # 美化打印
# You can also serialise to a Unicode string without declaration by
# passing the ``unicode`` function as encoding (or ``str`` in Py3),
# or the name 'unicode'. This changes the return value from a byte
# string to an unencoded unicode string.
print(etree.tostring(root, encoding=str, pretty_print=True)) #py3 使之返回 text
print(etree.tostring(root, encoding=unicode, pretty_print=True)) #py2 使之返回 unicode
1.3 自动补全
In [109]: rt = etree.HTML('<html><p>123</p></html>') #自动补全
In [110]: print(etree.tostring(rt, encoding=str, pretty_print=True))
<html>
<body>
<p>123</p>
</body>
</html>
1.4 fromstring 不支持残缺片段,不会自动补全
In [115]: rt = etree.fromstring('<html><p>456</html>') #fromstring 不支持残缺片段,不会自动补全
XMLSyntaxError: Opening and ending tag mismatch: p line 1 and html, line 1, column 20
In [116]: rt = etree.fromstring('<html><p>456</p></html>')
In [117]: print(etree.tostring(rt, encoding=str, pretty_print=True))
<html>
<p>456</p>
</html>
.
lxml.etree.HTML(text) 解析HTML文档的更多相关文章
- 网络电视精灵~分析~~~~~~简单工厂模式,继承和多态,解析XML文档,视频项目
小总结: 所用技术: 01.C/S架构,数据存储在XML文件中 02.简单工厂模式 03.继承和多态 04.解析XML文档技术 05.深入剖析内存中数据的走向 06.TreeView控件的使用 核心: ...
- 使用dom4j解析XML文档
dom4j的包开源包,不属于JDK里面,在myeclipse中要单独导入在项目中,这里不累赘了 做这个过程,很慢,因为很多方法没用过不熟悉,自己得去查帮助文档,而且还得去试,因为没有中文版,英文翻译不 ...
- 四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)
众所周知,现在解析XML的方法越来越多,但主流的方法也就四种,即:DOM.SAX.JDOM和DOM4J 下面首先给出这四种方法的jar包下载地址 DOM:在现在的Java JDK里都自带了,在xml- ...
- java 解析XML文档
Java 解析XML文档 一.解析XML文档方式: 1.DOM方式:将整个XML文档读取到内存中,按照XML文件的树状结构图进行解析. 2.SAX方式:基于事件的解析,只需要加载XML中的部分数据,优 ...
- Android解析Excel文档完整示例
MainActivity如下: package cc.testexcel; import java.io.File; import jxl.Cell; import jxl.CellType; imp ...
- DOM4J解析XML文档
Tip:DOM4J解析XML文档 Dom4j是一个简单.灵活的开放源代码的库.Dom4j是由早期开发JDOM的人分离出来而后独立开发的.与JDOM不同的是,dom4j使用接口和抽象基类,虽然Dom4j ...
- Java解析word文档
背景 在互联网教育行业,做内容相关的项目经常碰到的一个问题就是如何解析word文档. 因为系统如果无法智能的解析word,那么就只能通过其他方式手动录入word内容,效率低下,而且人工成本和录入出错率 ...
- python 解析docx文档的方法,以及利用Python从docx文档提取插入的文本对象和图片
首先安装docx模块,通过pip install docx或者在docx官方链接上下载安装都可以 下面来看下如何解析docx文档:文档格式如下 有3个部分组成 1 正文:text文档 2 一个表格. ...
- python之HTMLParser解析HTML文档
HTMLParser是Python自带的模块,使用简单,能够很容易的实现HTML文件的分析.本文主要简单讲一下HTMLParser的用法. 使用时需要定义一个从类HTMLParser继承的类,重定义函 ...
随机推荐
- vs2017开发IOS(vs2017 xamarin 连接mac)
这两天突然记起巨软的Visual Studio 2017 好像有说可以用C#开发IOS和Android应用,所以就自己去尝试了下生成过程. 相对与IOS来说,Android的生成过程还是比较容易的没有 ...
- MySQL索引管理
一.索引介绍 1.什么是索引 1.索引好比一本书的目录,它能让你更快的找到自己想要的内容. 2.让获取的数据更有目的性,从而提高数据库索引数据的性能. 2.索引类型介绍 1.BTREE:B+树索引 2 ...
- Mdoelsim10.4怎么脚本单独仿真ISE14.7 IP核
软件版本: Modelsim10.4SE ISE14.7 仿真IP:时钟管理IP(clock wizard) 流程: 1.对于Modelsim10.4SE,并不自带Xilinx家的仿真库,因此首先 ...
- hdu 5536 Chip Factory (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题面; Chip Factory Time Limit: 18000/9000 MS (Java/O ...
- SyntaxError: Unexpected token ' in JSON at position 2
js中字符串转json对象时报错: Uncaught SyntaxError: Unexpected token s in JSON at position 2 解决方法: js中获取jsp的返回值 ...
- Cucumber常用关键字
常用关键字(中英文对应) 对应的测试用例 Feature(功能) test suite (测试用例集) background(背景) Scenario(场景) test case(测试用例) Sc ...
- (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- openstack项目【day23】:keystone组件基础
本节内容 一 什么是keystone 二 为何要有keystone 三 keystone的功能 四 keystone概念详解 五 keystone内包含的组件 六 keystone与openstack ...
- 第十三节: EF的三种模式(三) 之 来自数据库的CodeFirst模式
一. 简介 [来自数据库的Code First模式]实质上并不是CodeFirst模式,而是DBFirst模式的轻量级版本,在该模式中取消了edmx模型和T4模板,直接生成了EF上下文和相应的类,该模 ...
- 关于缓存和 Chrome 的“新版刷新”
在读本文前你要确保读过我的上篇文章<扼杀 304,Cache-Control: immutable>,因为本文是接着上文写的.上文说到,在现代 Web 上,“条件请求/304 响应”绝大多 ...