脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python中PyQuery库用法分享

python中PyQuery库用法分享

2021-08-26 00:06十一 Python

在本篇文章里小编给大家整理了一篇关于python中PyQuery库用法内容,有兴趣的朋友们参考下。

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

安装:

?
1
>>> pip3 install pyquery

引用方法:

?
1
from pyquery import PyQuery as pq

CSS选择器:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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库用法分享

到此这篇关于python中PyQuery库用法分享的文章就介绍到这了,更多相关python中PyQuery库是什么内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.py.cn/jishu/jichu/22973.html

延伸 · 阅读

精彩推荐