python解析html开发库pyquery使用方法

 更新时间:2014年02月07日 09:59:11   投稿:zxhpj  
PyQuery是一个类似于jQuery的Python库,也可以说是jQuery在Python上的实现,能够以jQuery的语法来操作解析 HTML 文档,易用性和解析速度都很好

例如

复制代码 代码如下:

<div id="info">
<span><span class='pl'>导演</span>: <a href="/celebrity/1047989/" rel="v:directedBy">汤姆·提克威</a> / <a href="/celebrity/1161012/" rel="v:directedBy">拉娜·沃卓斯基</a> / <a href="/celebrity/1013899/" rel="v:directedBy">安迪·沃卓斯基</a></span><br/>
<span><span class='pl'>编剧</span>: <a href="/celebrity/1047989/">汤姆·提克威</a> / <a href="/celebrity/1013899/">安迪·沃卓斯基</a> / <a href="/celebrity/1161012/">拉娜·沃卓斯基</a></span><br/>
<span><span class='pl'>主演</span>: <a href="/celebrity/1054450/" rel="v:starring">汤姆·汉克斯</a> / <a href="/celebrity/1054415/" rel="v:starring">哈莉·贝瑞</a> / <a href="/celebrity/1019049/" rel="v:starring">吉姆·布劳德本特</a> / <a href="/celebrity/1040994/" rel="v:starring">雨果·维文</a> / <a href="/celebrity/1053559/" rel="v:starring">吉姆·斯特吉斯</a> / <a href="/celebrity/1057004/" rel="v:starring">裴斗娜</a> / <a href="/celebrity/1025149/" rel="v:starring">本·卫肖</a> / <a href="/celebrity/1049713/" rel="v:starring">詹姆斯·达西</a> / <a href="/celebrity/1027798/" rel="v:starring">周迅</a> / <a href="/celebrity/1019012/" rel="v:starring">凯斯·大卫</a> / <a href="/celebrity/1201851/" rel="v:starring">大卫·吉雅西</a> / <a href="/celebrity/1054392/" rel="v:starring">苏珊·萨兰登</a> / <a href="/celebrity/1003493/" rel="v:starring">休·格兰特</a></span><br/>
<span class="pl">类型:</span> <span property="v:genre">剧情</span> / <span property="v:genre">科幻</span> / <span property="v:genre">悬疑</span><br/>
<span class="pl">官方网站:</span> <a href="http://cloudatlas.warnerbros.com" rel="nofollow" target="_blank">cloudatlas.warnerbros.com</a><br/>
<span class="pl">语言:</span> 英语<br/>
<span class="pl">IMDb链接:</span> <a href="http://www.imdb.com/title/tt1371111" target="_blank" rel="nofollow">tt1371111</a><br>
<span class="pl">官方小站:</span>
<a href="http://site.douban.com/202494/" target="_blank">电影《云图》</a>
</div>

复制代码 代码如下:

from pyquery import PyQuery as pq
doc=pq(url='http://movie.douban.com/subject/3530403/')
data=doc('.pl')
for i in data:
    print pq(i).text()

输出

复制代码 代码如下:

导演
编剧
主演
类型:
官方网站:
制片国家/地区:
语言:
上映日期:
片长:
IMDb链接:
官方小站:

用法

用户可以使用PyQuery类从字符串、lxml对象、文件或者url来加载xml文档:

复制代码 代码如下:

>>> from pyquery import PyQuery as pq
>>> from lxml import etree
>>> doc=pq("<html></html>")
>>> doc=pq(etree.fromstring("<html></html>"))
>>> doc=pq(filename=path_to_html_file)
>>> doc=pq(url='http://movie.douban.com/subject/3530403/')

可以像jQuery一样选择对象了

复制代码 代码如下:

>>> doc('.pl')
[<span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span#rateword.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <p.pl>]

这样,class为'pl'的对象就全部选择出来了。

不过在使用迭代时需要对文本进行重新封装:

复制代码 代码如下:

for para in doc('.pl'):
    para=pq(para)
    print para.text()  
导演
编剧
主演
类型:
官方网站:
制片国家/地区:
语言:
上映日期:
片长:
IMDb链接:
官方小站:

这里得到的text是unicode码,如果要写入文件需要编码为字符串。
用户可以使用jquery提供的一些伪类(但还不支持css)来进行操作,诸如:

复制代码 代码如下:

>>> doc('.pl:first')
[<span.pl>]
>>> print  doc('.pl:first').text()
导演

Attributes
获取html元素的属性

复制代码 代码如下:

>>> p=pq('<p id="hello" class="hello"></p>')('p')
>>> p.attr('id')
'hello'
>>> p.attr.id
'hello'
>>> p.attr['id']
'hello'

赋值

复制代码 代码如下:

>>> p.attr.id='plop'
>>> p.attr.id
'plop'
>>> p.attr['id']='ola'
>>> p.attr.id
'ola'
>>> p.attr(id='hello',class_='hello2')
[<p#hello.hell0>]

Traversing
过滤

复制代码 代码如下:

>>> d=pq('<p id="hello" class="hello"><a/>hello</p><p id="test"><a/>world</p>')
>>> d('p').filter('.hello')
[<p#hello.hello>]
>>> d('p').filter('#test')
[<p#test>]
>>> d('p').filter(lambda i:i==1)
[<p#test>]
>>> d('p').filter(lambda i:i==0)
[<p#hello.hello>]
>>> d('p').filter(lambda i:pq(this).text()=='hello')
[<p#hello.hello>]

按照顺序选择

复制代码 代码如下:

>>> d('p').eq(0)
[<p#hello.hello>]
>>> d('p').eq(1)
[<p#test>]

选择内嵌元素

复制代码 代码如下:

>>> d('p').eq(1).find('a')
[<a>]

选择父元素

复制代码 代码如下:

>>> d=pq('<p><span><em>Whoah!</em></span></p><p><em> there</em></p>')
>>> d('p').eq(1).find('em')
[<em>]
>>> d('p').eq(1).find('em').end()
[<p>]
>>> d('p').eq(1).find('em').end().text()
'there'
>>> d('p').eq(1).find('em').end().end()
[<p>, <p>]

相关文章

  • 详解python变量的命名和使用

    详解python变量的命名和使用

    变量名只能包含字母、数字和下划线,本文主要介绍了详解python变量的命名和使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-12-12
  • python实现拓扑排序的基本教程

    python实现拓扑排序的基本教程

    拓扑排序是对有向无环图的一种排序,发现自己并没有真的理解拓扑排序,再次学习了下,所以下面这篇文章主要给大家介绍了关于python实现拓扑排序的基本教程,文中通过示例代码介绍的非常详细,需要的朋友可以参考下,
    2018-03-03
  • certifi轻松地管理Python证书信任链保障网络安全

    certifi轻松地管理Python证书信任链保障网络安全

    在使用Python进行网络通信时,我们通常需要使用第三方库来处理HTTPS连接,其中,certifi库是一个非常实用的库,可以帮助我们轻松地管理Python的证书信任链
    2024-01-01
  • Python Pandas创建Dataframe数据框的六种方法汇总

    Python Pandas创建Dataframe数据框的六种方法汇总

    这篇文章主要介绍了Python中的Pandas创建Dataframe数据框的六种方法,创建Dataframe主要是使用pandas中的DataFrame函数,其核心就是第一个参数:data,传入原始数据,因此我们可以据此给出六种创建Dataframe的方法,需要的朋友可以参考下
    2023-05-05
  • Python中 CSV格式清洗与转换的实例代码

    Python中 CSV格式清洗与转换的实例代码

    这篇文章主要介绍了Python123 CSV格式清洗与转换的实例代码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08
  • python中执行shell的两种方法总结

    python中执行shell的两种方法总结

    这篇文章主要介绍了python中执行shell的两种方法,有两种方法可以在Python中执行SHELL程序,方法一是使用Python的commands包,方法二则是使用subprocess包,这两个包均是Python现有的内置模块。需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-01-01
  • 在python 中实现运行多条shell命令

    在python 中实现运行多条shell命令

    今天小编就为大家分享一篇在python 中实现运行多条shell命令,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • Python判断文件和字符串编码类型的实例

    Python判断文件和字符串编码类型的实例

    下面小编就为大家分享一篇Python判断文件和字符串编码类型的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • python实现识别相似图片小结

    python实现识别相似图片小结

    本文给大家分享的是使用Python实现图片相似度识别的总结,代码实用pil模块比较两个图片的相似度,根据实际实用,代码虽短但效果不错,还是非常靠谱的。
    2016-02-02
  • TensorFlow2.0使用keras训练模型的实现

    TensorFlow2.0使用keras训练模型的实现

    这篇文章主要介绍了TensorFlow2.0使用keras训练模型的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02

最新评论