Python lxml 使用
2024-09-07 13:46:17
lxml,是python中用来处理xml和html的功能最丰富和易用的库
from lxml import etree
from lxml import html h = ''' <html>
<head>
<meta name="content-type" content="text/html; charset=utf-8" />
<title>友情链接查询 - 站长工具</title>
<!-- uRj0Ak8VLEPhjWhg3m9z4EjXJwc -->
<meta name="Keywords" content="友情链接查询" />
<meta name="Description" content="友情链接查询" /> </head>
<body>
<h1 class="heading">Top News</h1>
<p style="font-size: 200%">World News only on this page</p>
Ah, and here's some more text, by the way.
<p>... and this is a parsed fragment ...</p> <a href="http://www.cydf.org.cn/" rel="nofollow" target="_blank">青少年发展基金会</a>
<a href="http://www.4399.com/flash/32979.htm" target="_blank">洛克王国</a>
<a href="http://www.4399.com/flash/35538.htm" target="_blank">奥拉星</a>
<a href="http://game.3533.com/game/" target="_blank">手机游戏</a>
<a href="http://game.3533.com/tupian/" target="_blank">手机壁纸</a>
<a href="http://www.4399.com/" target="_blank">4399小游戏</a>
<a href="http://www.91wan.com/" target="_blank">91wan游戏</a> </body>
</html> '''
# 第一种使用方法
page = etree.HTML(h)
#hrefs = page.xpath('//a')
href = page.cssselect('a')
for href in hrefs:
print(href.attrib) 第二种使用方法
def parse_from():
tree = html.fromstring(h)
for href in tree.cssselect('a'):
#for hfre in tree.xpath('//a'):
a = href
print(a.text)
print(a.attrib) paese_from() parse_from()
最新文章
- Xcode 8.2 想使用插件 怎么办? 教你科学的使用插件
- 搭建appium的android环境
- CoreData (表结构变化处理)
- C# 之 Word光标移动 GoTo 方法
- The partner transaction manager has disabled its support for remote/network transactions.
- plsql 高效原则
- Java反序列化
- SETLOCAL
- .\Obj\main.axf: Error: L6406E: No space in execution regions with .ANY selector matching sin_i.o(.co
- XFS:大数据环境下Linux文件系统的未来?
- 一些安全相关的HTTP响应头
- Scikit-learn:分类classification
- 第一次C语言程序设计
- C#中MemoryStream类的介绍
- 浅谈jQuery的promise
- iOS 技术篇:渐变色的简单实现方式
- MySQL Sandbox安装使用
- Spring Shell介绍
- React (native) 相关知识
- Linux chmod和chown更改文件目录的所属者命令的用法
热门文章
- chrome扩展(浏览器插件)开发实用教程
- svn:database disk image is malformed问题解决方法
- gitlab 把或名改成IP
- linux学习之vimrc配置推荐
- IOS设计模式浅析之外观模式(Facade)
- 着手打造你的随身系统---将linux装进移动硬盘
- Python 字典初始化dict()和{}
- VC++通过API连接MySQL
- oracle ora-01652/oracle表空间
- 抒发一下这些天用django做web项目的一些体会