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

在放大镜下,你可以看到每一片雪花都是一幅幅精美的图案:有的是晶莹的薄片,有的像白亮的银针,有的像一把张开的小扇,有的像夜空的星星……

例如


<div id="info">
<span><span class='pl'>导演</span>: <a rel="nofollow noopener noreferrer" href="/celebrity/1047989/" rel="v:directedBy">汤姆·提克威</a> / <a rel="nofollow noopener noreferrer" href="/celebrity/1161012/" rel="v:directedBy">拉娜·沃卓斯基</a> / <a rel="nofollow noopener noreferrer" href="/celebrity/1013899/" rel="v:directedBy">安迪·沃卓斯基</a></span><br/>
<span><span class='pl'>编剧</span>: <a rel="nofollow noopener noreferrer" href="/celebrity/1047989/">汤姆·提克威</a> / <a rel="nofollow noopener noreferrer" href="/celebrity/1013899/">安迪·沃卓斯基</a> / <a rel="nofollow noopener noreferrer" href="/celebrity/1161012/">拉娜·沃卓斯基</a></span><br/>
<span><span class='pl'>主演</span>: <a rel="nofollow noopener noreferrer" href="/celebrity/1054450/" rel="v:starring">汤姆·汉克斯</a> / <a rel="nofollow noopener noreferrer" href="/celebrity/1054415/" rel="v:starring">哈莉·贝瑞</a> / <a rel="nofollow noopener noreferrer" href="/celebrity/1019049/" rel="v:starring">吉姆·布劳德本特</a> / <a rel="nofollow noopener noreferrer" href="/celebrity/1040994/" rel="v:starring">雨果·维文</a> / <a rel="nofollow noopener noreferrer" href="/celebrity/1053559/" rel="v:starring">吉姆·斯特吉斯</a> / <a rel="nofollow noopener noreferrer" href="/celebrity/1057004/" rel="v:starring">裴斗娜</a> / <a rel="nofollow noopener noreferrer" href="/celebrity/1025149/" rel="v:starring">本·卫肖</a> / <a rel="nofollow noopener noreferrer" href="/celebrity/1049713/" rel="v:starring">詹姆斯·达西</a> / <a rel="nofollow noopener noreferrer" href="/celebrity/1027798/" rel="v:starring">周迅</a> / <a rel="nofollow noopener noreferrer" href="/celebrity/1019012/" rel="v:starring">凯斯·大卫</a> / <a rel="nofollow noopener noreferrer" href="/celebrity/1201851/" rel="v:starring">大卫·吉雅西</a> / <a rel="nofollow noopener noreferrer" href="/celebrity/1054392/" rel="v:starring">苏珊·萨兰登</a> / <a rel="nofollow noopener noreferrer" 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 rel="nofollow noopener noreferrer" 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 rel="nofollow noopener noreferrer" href="http://www.imdb.com/title/tt1371111" target="_blank" rel="nofollow">tt1371111</a><br>
<span class="pl">官方小站:</span>
<a rel="nofollow noopener noreferrer" 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解析html开发库pyquery使用方法。愚者用肉体监督心灵,智者专心灵监督肉体。更多关于python解析html开发库pyquery使用方法请关注haodaima.com其它相关文章!

标签: python html