python中PyQuery库用法分享

脚本专栏 发布日期:2024/10/8 浏览次数:1

正在浏览:python中PyQuery库用法分享

PyQuery库就是python中的一个解析库。作用十分强大,使用上也是相当的灵活,能够实现初始化字符串、初始化 HTML 文件、初始化请求响应等多种场景,另外,本身中的CSS选择器,也是经常被大家使用的对象,结合以上的大致内容总结,接下来,我们一起详细的了解使用内容。

安装:

> pip3 install pyquery

引用方法:

from pyquery import PyQuery as pq

CSS选择器:

from pyquery import PyQuery as pq
html = '''
<div id="container">
<ul class="list">
<li class="item-0">first item</li>
<li class="item-1"><a href="link2.html" rel="external nofollow" >second item</a></li>
<li class="item-0 active"><a href="link3.html" rel="external nofollow" ><span class="bold">third item</span></a></li>
<li class="item-1 active"><a href="link4.html" rel="external nofollow" >fourth item</a></li>
<li class="item-0"><a href="link5.html" rel="external nofollow" >fifth item</a></li>
</ul>
</div>
'''
doc=pq(html)
print(doc("#container .list li"))

输出结果:

python中PyQuery库用法分享