【BOOK】解析库--pyquery
CSS选择器
1、初始化
html='''
<div>
<ul>
<li class="item-0">first item</li>
<li class="item-1"><a href="link2.html">second item</a></li>
<li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
<li class="item-1 active"><a href="link4.html">fourth item</a></li>
<li class="item-0 "><a href="link5.html">fifth item</a></li>
</ul>
</div>
''' from pyquery import PyQuery as pq
## 字符串初始化
doc = pq(html)
print(doc('li')) ## URL初始化
doc= pq(url='https://cuiqingcai.com')
print(doc('title')) ## 文件初始化
doc = pq(filename='test.html')
print(doc('li'))
2、CSS选择器
# CSS选择器
from pyquery import PyQuery as pq
doc = pq(html)
## id用 #,class用 .
print(doc('#container .list li'))
3、查找节点
html='''
<div id="container">
<ul class="list">
<li class="item-0">first item</li>
<li class="item-1"><a href="link2.html">second item</a></li>
<li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
<li class="item-1 active"><a href="link4.html">fourth item</a></li>
<li class="item-0 "><a href="link5.html">fifth item</a></li>
</ul>
</div>
''' ##查找节点
from pyquery import PyQuery as pq
doc = pq(html)
items = doc('.list')
print(items) ## find() 查找所有地子孙节点
lis = items.find('a')
print(lis) ## children() 只查找子节点
lis = items.children('.active')
print(lis) ## parent() 查找父节点
container = items.parent()
print(container) ## parents() 查找祖先节点
ancestor = items.parents()
print(ancestor) ## siblings() 查找兄弟节点
li = doc('.item-0.active')
print(li.siblings('.active'))
4、遍历
from pyquery import PyQuery as pq
doc = pq(html)
## 调用items()得到一个生成器,for in 进行遍历
lis = doc('li').items()
for li in lis:
print(li)
5、获取内容
from pyquery import PyQuery as pq
doc = pq(html)
a = doc('.item-0 a')
print(a)
## 获取 a节点的 href的属性值
## attr()只返回第一个结果
print(a.attr('href'))
print(a.attr.href)
## 通过遍历获取所有的属性值
for item in a.items():
print(item.attr('href'))
## 只获取文本 多个节点的文本内容用 空格 间隔开
print(a.text()) ## third item fifth item ## 获取包含 a节点内部所有内容,包含节点,返回第一个 a节点 内部的HTML文本
print(a.html()) # <span class="bold">third item</span>
6、节点操作
## 节点操作
## remove() html ='''
<div class="wrap">
Hello World
<p>呱呱呱</p>
</div>
'''
## 只想获得Hello World
from pyquery import PyQuery as pq
doc = pq(html)
wrap = doc('.wrap')
## 移除 p节点
wrap.find('p').remove()
print(wrap.text())
7、伪类选择器
html='''
<div id="container">
<ul class="list">
<li class="item-0">first item</li>
<li class="item-1"><a href="link2.html">second item</a></li>
<li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
<li class="item-1 active"><a href="link4.html">fourth item</a></li>
<li class="item-0 "><a href="link5.html">fifth item</a></li>
</ul>
</div>
''' from pyquery import PyQuery as pq
doc = pq(html) ## 第一个li节点
li = doc('li:first-child')
print(li) ## 最后一个li节点
li = doc('li:last-child')
print(li) ## 第2个li节点
li = doc('li:nth-child(2)')
print(li) ## 包含‘second’文本的li节点
li = doc('li:contains(second)')
print(li)
【BOOK】解析库--pyquery的更多相关文章
- python3解析库pyquery
pyquery是一个类似jquery的python库,它实现能够在xml文档中进行jQuery查询,pyquery使用lxml解析器进行快速在xml和html文档上操作,它提供了和jQuery类似的语 ...
- 小白学 Python 爬虫(23):解析库 pyquery 入门
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- Python的网页解析库-PyQuery
PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQuery 是 Python 仿照 jQuery 的严 ...
- Python3编写网络爬虫07-基本解析库pyquery的使用
三.pyquery 简介:同样是一个强大的网页解析工具 它提供了和jQuery类似的语法来解析HTML文档,支持CSS选择器,使用非常方便 安装: pip install pyquery 验证: im ...
- 爬虫之解析库pyquery
初始化 安装: pip install pyquery 字符串的形式初始化 html = """ <html lang="en"> < ...
- pyquery 的用法 --爬虫解析库
如果你对Web有所涉及,如果你比较喜欢用CSS选择器,如果你对jQuery有所了解,那么这里有一个更适合你的解析库--pyquery. 接下来,我们就来感受一下pyquery的强大之处. 1. 准备工 ...
- Python爬虫【解析库之pyquery】
该库跟jQuery的使用方法基本一样 http://pyquery.readthedocs.io/ 官方文档 解析库的安装 pip3 install pyquery 初始化 1.字符串初始化 htm ...
- 【Python爬虫】PyQuery解析库
PyQuery解析库 阅读目录 初始化 基本CSS选择器 查找元素 遍历 获取信息 DOM操作 伪类选择器 PyQuery 是 Python 仿照 jQuery 的严格实现.语法与 jQuery 几乎 ...
- (最全)Xpath、Beautiful Soup、Pyquery三种解析库解析html 功能概括
一.Xpath 解析 xpath:是一种在XMl.html文档中查找信息的语言,利用了lxml库对HTML解析获取数据. Xpath常用规则: nodename :选取此节点的所有子节点 // : ...
- Python3 BeautifulSoup和Pyquery解析库随笔
BeautifuSoup和Pyquery解析库方法比较 1.对象初始化: BeautifySoup库: from bs4 import BeautifulSoup html = 'html strin ...
随机推荐
- 如何修改驱动使得NVIDIA Geforce GTX 970, 980, 980 TI and Titan X等显卡可以在Win XP/Win 2003 server x64下驱动?
感谢Matt,一个老外,非常好的修改方法. 本人亲测成功. I recently built a new computer to better accommodate the forthcoming ...
- shell脚本实战笔录-在PATH中查找程序
#!/bin/bashin_path(){ cmd=$1 ourpath=$2 result=1 #将默认的IFS(分隔符)为空格,这里先保留原本的给变量oldIFS,然 ...
- 1. mongodb基础:cursor.forEach使用
mongosh下载地址: https://downloads.mongodb.com/compass/mongodb-mongosh-shared-openssl3-1.6.0.x86_64.rpm? ...
- 快速导出Redis某个List列表所有数据
Redis导出list数据 快速命令行 echo "lrange data_list_with_hash 0 25000" | ./redis-cli.exe -h 127.0.0 ...
- NGINX websocket 配制
http { map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstre ...
- wsl2 的安装与使用
wsl2 简介 wsl2 是 window 自家做的虚拟机,如果初次接触,可以建立的理解为 vmware.只不过他是 window 公司自己开发的,所以从兼容性上来讲,会更好一些. 我个人选择使用 w ...
- Java调试排错心得
首先这里没有报错,但是打印了四行相同的数据,还都是最后一行的数据.然后调试了一下 这里是重点: 下面哪里account = {Account@1580}是一直用的一个对象,所有每一次调试那些什么rs. ...
- 查看linux 用户
sudo cat /etc/passwd|grep -v nologin|grep -v halt|grep -v shutdown|awk -F":" '{ print $1}' ...
- 关键aspNetCore processPath 这一行,耗费了一天
<?xml version="1.0" encoding="UTF-8"?> <configuration> <locatio ...
- 服务器IPMI地址及用户名密码
HP管理口:ILO默认用户/密码:Administrator/passwordHP以前管理口登陆MP卡通过网线连接MP卡的RJ-45口,通过telnet方式登录,默认用户/密码:Admin/Admin ...