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

yipeiwu_com5年前Python基础

例如

复制代码 代码如下:

<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">语言:</span> 英语<br/>
<span class="pl">上映日期:</span> <span property="v:initialReleaseDate" content="2013-01-31(中国大陆)">2013-01-31(中国大陆)</span> / <span property="v:initialReleaseDate" content="2012-10-26(美国)">2012-10-26(美国)</span><br/>
<span class="pl">片长:</span> <span property="v:runtime" content="134">134分钟(中国大陆)</span> / 172分钟(美国)<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+pyqt5实现图片批量缩放工具

python+pyqt5实现图片批量缩放工具

批量修改图片大小好像用PS也可以,不过我不会,程序猿就用程序来解决。 这段时间学了下Python,很强大,之前一些不知道怎么处理的东西在Python里面都能找到解决方法。 工具界面如下...

Python使用smtp和pop简单收发邮件完整实例

SMTP SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。 Python对SMTP支持有smtplib和email两个模块,...

python中反射用法实例

本文实例讲述了python中反射用法。分享给大家供大家参考。具体如下: import sys, types,new def _get_mod(modulePath): try:...

Python中动态创建类实例的方法

简介 在Java中我们可以通过反射来根据类名创建类实例,那么在Python我们怎么实现类似功能呢? 其实在Python有一个builtin函数import,我们可以使用这个函数来在运行时...

Python迭代器iterator生成器generator使用解析

1. 迭代 根据记录的前面的元素的位置信息 去访问后续的元素的过程 -遍历 迭代 2. 可迭代对象 iterable 如何判断可迭代对象的3种方式 能够被迭代访问的对象 for...