这篇文章主要介绍了Python爬虫PyQuery库基本用法,结合实例形式较为详细的分析了pyQuery库字符串初始化、打开网页、css属性、标签内容等获取、DOM基本操作等相关技巧与使用注意事项,需要的朋友可以参考下
本文实例讲述了Python爬虫PyQuery库基本用法。分享给大家供大家参考,具体如下:
PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQuery 是 Python 仿照 jQuery 的严格实现。语法与 jQuery 几乎完全相同,所以不用再去费心去记一些奇怪的方法了。
官网地址:http://pyquery.readthedocs.io/en/latest/
jQuery参考文档: http://jquery.cuishifeng.cn/
1、字符串的初始化
from pyquery import PyQuery as pq html = '''''' doc = pq(html) print(doc) print(type(doc)) print(doc('li'))
- first item
- second item
- third item
- fourth item
- fifth item
first item
second item
third item
fourth item
fifth item
2、打开html文件
注意路劲问题
from pyquery import PyQuery as pq doc = pq(filename='index.html') print(doc) print(doc('head'))
Title
'''
- first item
- second item
- third item
- fourth item
- fifth item