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()

  

最新文章

  1. Xcode 8.2 想使用插件 怎么办? 教你科学的使用插件
  2. 搭建appium的android环境
  3. CoreData (表结构变化处理)
  4. C# 之 Word光标移动 GoTo 方法
  5. The partner transaction manager has disabled its support for remote/network transactions.
  6. plsql 高效原则
  7. Java反序列化
  8. SETLOCAL
  9. .\Obj\main.axf: Error: L6406E: No space in execution regions with .ANY selector matching sin_i.o(.co
  10. XFS:大数据环境下Linux文件系统的未来?
  11. 一些安全相关的HTTP响应头
  12. Scikit-learn:分类classification
  13. 第一次C语言程序设计
  14. C#中MemoryStream类的介绍
  15. 浅谈jQuery的promise
  16. iOS 技术篇:渐变色的简单实现方式
  17. MySQL Sandbox安装使用
  18. Spring Shell介绍
  19. React (native) 相关知识
  20. Linux chmod和chown更改文件目录的所属者命令的用法

热门文章

  1. chrome扩展(浏览器插件)开发实用教程
  2. svn:database disk image is malformed问题解决方法
  3. gitlab 把或名改成IP
  4. linux学习之vimrc配置推荐
  5. IOS设计模式浅析之外观模式(Facade)
  6. 着手打造你的随身系统---将linux装进移动硬盘
  7. Python 字典初始化dict()和{}
  8. VC++通过API连接MySQL
  9. oracle ora-01652/oracle表空间
  10. 抒发一下这些天用django做web项目的一些体会