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

  

Python lxml 使用的更多相关文章

  1. python笔记:windows 下安装 python lxml

    原文:http://blog.csdn.net/zhaokuo719/article/details/8209496 windows 环境下安装 lxml python 1.首先保证你的python ...

  2. python lxml install

    之前记得安装libxslt和libxml yum install libxml* -yyum install libxslt* -y wget http://lxml.de/files/lxml-3. ...

  3. Windows下安装Python lxml库(无废话版)

    python官网:python-2.7.12.amd64.msihttps://pypi.python.org/pypi/setuptools:setuptools-28.6.0.zipsetupto ...

  4. python lxml教程

    目前有很多xml,html文档的parser,如标准库的xml.etree , beautifulsoup  ,  还有lxml. 都用下来感觉lxml不错,速度也还行,就他了. 围绕三个问题: 问题 ...

  5. python lxml库生成xml文件-节点命名空间问题

    lxml库,处理xml很强大,官方文档:https://lxml.de/tutorial.html#namespaces 例如: 我们要生成如下格式的报文: <ttt:jesson xmlns: ...

  6. python处理xml的常用包(lib.xml、ElementTree、lxml)

    python处理xml的三种常见机制 dom(随机访问机制) sax(Simple APIs for XML,事件驱动机制) etree python处理xml的三种包 标准库中的xml Fredri ...

  7. python网络爬虫之LXML与HTMLParser

    Python lxml包用于解析html和XML文件,个人觉得比beautifulsoup要更灵活些 Lxml中的路径表达式如下: 在下面的表格中,我们已列出了一些路径表达式以及表达式的结果: 路径表 ...

  8. python爬微信公众号前10篇历史文章(3)-lxml&xpath初探

    理解lxml以及xpath 什么是lxml? python中用来处理XML和HTML的library.与其他相比,它能提供很好的性能, 并且它支持XPath. 具体可以查看官方文档->http: ...

  9. Python爬虫基础之lxml

    一.Python lxml的基本应用 <html> <head> <title> The Dormouse's story </title> </ ...

随机推荐

  1. Android应用TranslateAnimation移动之后,利用视图的setLayoutPara

    Android中利用TranslateAnimation移动时,不设置mTranslateAnimation.setFillAfter(true);,而利用视图的setLayoutParams来重新定 ...

  2. CKEditor的安装与基本使用(JSP)

    文章分类:Web前端 一.下载CKEditor 1. 直接下载地址.当前最新版本号为3.5: http://download.cksource.com/CKEditor/CKEditor/CKEdit ...

  3. .net Lock用法(转)

    lock就是把一段代码定义为临界区,所谓临界区就是同一时刻只能有一个线程来操作临界区的代码,当一个线程位于代码的临界区时,另一个线程不能进入临界区,如果试图进入临界区,则只能一直等待(即被阻止),直到 ...

  4. 28. Search a 2D Matrix 【easy】

    28. Search a 2D Matrix [easy] Write an efficient algorithm that searches for a value in an mx n matr ...

  5. 键值对集合 dict(字典)

    xx= { ss, ss } 创建字典 len(ss) 返回字典到长度,len函数可以返回任何集合的长度,list.tuple.dict都是集合的一种 什么是dict 我们已经知道,list 和 tu ...

  6. LeetCode459. Repeated Substring Pattern

    Description Given a non-empty string check if it can be constructed by taking a substring of it and ...

  7. 【转】 详细介绍windows下使用python pylot进行网站压力测试

    windows下使用python进行网站压力测试,有两个必不可少的程序需要安装,一个是python,另一个是pylot.python是一个安装软件,用来运行python程序,而pylot则是pytho ...

  8. Eclipse 菜单

    Eclipse 菜单 Eclipse 查看的菜单栏通常包含以下几个菜单: File 菜单 Edit 菜单 Navigate 菜单 Search 菜单 Project 菜单 Run 菜单 Window ...

  9. Python 之 ImportError: No module named ***

    假设想使用非当前模块中的代码,须要使用Import.这个大家都知道. 假设你要使用的模块(py文件)和当前模块在同一文件夹.仅仅要import对应的文件名称就好,比方在a.py中使用b.py: imp ...

  10. 关于swift 单元测试的补充

    最近小弟在自己学习研究swift , 习惯于写一边写单元测试一边写逻辑的我来说,在xcode环境没有单元测试,写起来就是有个不是实在的感觉. 至于怎么创建单元测试.怎么写,不是这个文章的主题,因为看了 ...