网络爬虫学习笔记
课程名称:52讲学爬虫
1.9 爬虫解析器PyQuery
相关链接
CSS选择器:https://www.w3school.com.cn/cssref/css_selectors.asp
PyQuery官方文档:heep://pyqery.readthedocs,io
安装pip install pyquery
使用样例
1 | #传入HTML string |
1 | #传入URL |
1 | #传入本地文件 |
基础CSS选择器
doc('#container .list li)
选取ID为container节点,再选取Class为list的节点,再选取li节点for item in doc('#container .list li').item()
后面所有方法使用时如果需要筛选,将CSS选择器作为参数传入即可
查询子孙节点
find
方法
1 | items = doc('.list') |
查询子节点
children
方法lis = item.childern('.active')
查询父节点
parent
方法lis = item.parent()
查询祖先节点
parents
方法lis = item.parents()
查询兄弟节点
siblings
方法lis = item.siblings()
获取节点属性
attr
方法
1 | a = doc('.active a') |
获取节点内容
text
方法a.text()
多个节点则返回所有对象的text,转化为用’ ‘隔开的字符串
获取节点HTML文本
html
方法(包含html标签)a.html()
节点操做
addClass & removeClass
动态改变节点Class属性li.addClass('active')
li.removeClass('active')
attr方法操做属性
多参数将添加或改变节点属性li.attr('name','link')
text & html操做内容
传入参数则改变对应值li.text('change text')
remove删除节点
li.remove()
伪类选择器
参考CSS官方文档
1.10MongoDB
C++编写的非关系性数据库,存储内容类似JSON,字段值支持其他文档,数组,文档数组