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的更多相关文章

  1. python3解析库pyquery

    pyquery是一个类似jquery的python库,它实现能够在xml文档中进行jQuery查询,pyquery使用lxml解析器进行快速在xml和html文档上操作,它提供了和jQuery类似的语 ...

  2. 小白学 Python 爬虫(23):解析库 pyquery 入门

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  3. Python的网页解析库-PyQuery

    PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQuery 是 Python 仿照 jQuery 的严 ...

  4. Python3编写网络爬虫07-基本解析库pyquery的使用

    三.pyquery 简介:同样是一个强大的网页解析工具 它提供了和jQuery类似的语法来解析HTML文档,支持CSS选择器,使用非常方便 安装: pip install pyquery 验证: im ...

  5. 爬虫之解析库pyquery

    初始化 安装: pip install pyquery 字符串的形式初始化 html = """ <html lang="en"> < ...

  6. pyquery 的用法 --爬虫解析库

    如果你对Web有所涉及,如果你比较喜欢用CSS选择器,如果你对jQuery有所了解,那么这里有一个更适合你的解析库--pyquery. 接下来,我们就来感受一下pyquery的强大之处. 1. 准备工 ...

  7. Python爬虫【解析库之pyquery】

    该库跟jQuery的使用方法基本一样  http://pyquery.readthedocs.io/ 官方文档 解析库的安装 pip3 install pyquery 初始化 1.字符串初始化 htm ...

  8. 【Python爬虫】PyQuery解析库

    PyQuery解析库 阅读目录 初始化 基本CSS选择器 查找元素 遍历 获取信息 DOM操作 伪类选择器 PyQuery 是 Python 仿照 jQuery 的严格实现.语法与 jQuery 几乎 ...

  9. (最全)Xpath、Beautiful Soup、Pyquery三种解析库解析html 功能概括

    一.Xpath 解析   xpath:是一种在XMl.html文档中查找信息的语言,利用了lxml库对HTML解析获取数据. Xpath常用规则: nodename :选取此节点的所有子节点 // : ...

  10. Python3 BeautifulSoup和Pyquery解析库随笔

    BeautifuSoup和Pyquery解析库方法比较 1.对象初始化: BeautifySoup库: from bs4 import BeautifulSoup html = 'html strin ...

随机推荐

  1. 记maven打包加入外部jar后tomcat运行失败问题

    环境:maven 3.8.0 .tomcat 8.5.30 因为项目需要额外的引用外部jar,需要打包到war中. 所以在pom中加入了 <webResources> <resour ...

  2. 关于MYSQL知识点复习

    关于MYSQL关联查询JOIN:   https://www.cnblogs.com/withscorpion/p/9454490.html

  3. -bash: pip: command not found

    使用pip安装软件包时报错命令不存在 [root@test ~]# pip -V -bash: pip: command not found 机器上没有安装pip,需要手动进行安装 centos系统: ...

  4. 国产DP4398 兼容替代CS4398 24Bit 192KHz数模转换芯片

    CS4398是一款24Bit/192K Hz规格的解码芯片,它具有120分贝以上的讯噪比和动态范围,采用一个高级专用多位Delta-Sigma调制器,并整合了失配噪声整形技术.今天来讲讲它的国产替代型 ...

  5. Redux Toolkit——基操

    redux-toolkit是redux的升级版 安装 npm install @reduxjs/toolkit // 在react中还需要搭配react-redux使用 npm install rea ...

  6. JSP图书管理系统

    allbook.jsp pageEncoding="UTF-8"%> <%@ page import = "java.util.*" %> & ...

  7. 怎么才能卸载inventor?完全彻底卸载删除干净inventor各种残留注册表和文件的方法和步骤

    怎么才能卸载inventor?完全彻底卸载删除干净inventor各种残留注册表和文件的方法和步骤.如何卸载inventor呢?有很多同学想把inventor卸载后重新安装,但是发现inventor安 ...

  8. webrtc windows编译记录

    //cmd set path=D:\zzh\depot_tools;%path% set DEPOT_TOOLS_WIN_TOOLCHAIN 0 set vs2022_install=C:\Progr ...

  9. 攻防世界-fileclude

    攻防世界的一道文件包含题目 include("文件名"):会将文件中的内容视为代码块接入include所在代码中,输出的只是执行后的结果,文件中的注释.定义等无法查看. 本题中可以 ...

  10. 生产环境自动备份win服务器所有web项目(IIS+项目代码)

    @echo offrem 功能:每月自动备份本服务器所有web项目rem 日期:2022.3.10rem 制作人:zl rem 定义变量Y为备份时间:YYYYMMset y=%date:~0,4%%d ...