### pyquery的介绍和使用

## 测试文本
text = '''
<html><head><title>there is money</title></head>
<body>
<p class="title" name="dmr"><b>there is money</b>contents</p>
<p class="money">good good study, day day up
<a href="https://www.baidu.com/1" class="error" id="l1"><span><!-- 1 --></span></a>,
<a href="https://www.baidu.com/2" class="error ed2" id="l2"><span>2</span></a> and
<a href="https://www.baidu.com/3" class="error" id="l3">3</a>;
66666666666
</p>
<p class='body'>...</p>
'''

1. pyquery对象初始化,html字符串,url,file皆可

## pyquery对象初始化,html字符串,url,file皆可
from pyquery import PyQuery as pq
import requests # html字符串初始化
doc = pq(text)
print(doc('a'))
# url初始化
doc = pq(requests.get('https://www.baidu.com').text)
print(doc('title'))
# 读取文件内容初始化,编码格式为GBK,当有不可识别字符时会报错,可通过open指定编码格式为utf-8来解决
# doc = pq(filename='text')
# print(doc('li'))

2. 基本CSS选择器

## 基本CSS选择器
from pyquery import PyQuery as pq doc = pq(text)
print(type(doc))
print(doc('.money a'))
print(doc('.money #l1'))
'''
输出内容:
<class 'pyquery.pyquery.PyQuery'>
<a href="https://www.baidu.com/1" class="error" id="l1"><span><!-- 1 --></span></a>,
<a href="https://www.baidu.com/2" class="error" id="l2"><span>2</span></a> and
<a href="https://www.baidu.com/3" class="error" id="l3">3</a>;
66666666666 <a href="https://www.baidu.com/1" class="error" id="l1"><span><!-- 1 --></span></a>,
'''

输出内容

3. 查找节点,返回一个PyQuery对象,当匹配到多个节点时,PyQuery对象值为多个节点的字符串整合

## 查找节点,返回一个PyQuery对象,当匹配到多个节点时,PyQuery对象值为多个节点的字符串整合
from pyquery import PyQuery as pq doc = pq(text)
items = doc('p')
print(items)
print(type(items))
# 查找子节点
print(items.children('#l2'))
print('--------------------分隔符------------------')
# 查找父节点
print(items.parent())
print('--------------------分隔符------------------')
print(items.parents('html'))
print('--------------------分隔符------------------')
## 查找兄弟节点
print(items('#l2').siblings())
'''
输出内容:
<p class="title" name="dmr"><b>there is money</b></p>
<p class="money">good good study, day day up
<a href="https://www.baidu.com/1" class="error" id="l1"><span><!-- 1 --></span></a>,
<a href="https://www.baidu.com/2" class="error" id="l2"><span>2</span></a> and
<a href="https://www.baidu.com/3" class="error" id="l3">3</a>;
66666666666
</p>
<p class="body">...</p> <class 'pyquery.pyquery.PyQuery'>
<a href="https://www.baidu.com/2" class="error" id="l2"><span>2</span></a> and --------------------分隔符------------------
<body>
<p class="title" name="dmr"><b>there is money</b></p>
<p class="money">good good study, day day up
<a href="https://www.baidu.com/1" class="error" id="l1"><span><!-- 1 --></span></a>,
<a href="https://www.baidu.com/2" class="error" id="l2"><span>2</span></a> and
<a href="https://www.baidu.com/3" class="error" id="l3">3</a>;
66666666666
</p>
<p class="body">...</p>
</body>
--------------------分隔符------------------
<html><head><title>there is money</title></head>
<body>
<p class="title" name="dmr"><b>there is money</b></p>
<p class="money">good good study, day day up
<a href="https://www.baidu.com/1" class="error" id="l1"><span><!-- 1 --></span></a>,
<a href="https://www.baidu.com/2" class="error" id="l2"><span>2</span></a> and
<a href="https://www.baidu.com/3" class="error" id="l3">3</a>;
66666666666
</p>
<p class="body">...</p>
</body></html>
--------------------分隔符------------------
<a href="https://www.baidu.com/1" class="error" id="l1"><span><!-- 1 --></span></a>,
<a href="https://www.baidu.com/3" class="error" id="l3">3</a>;
66666666666
'''

输出内容

4. 遍历,通过PyQuery对象的items方法可以把匹配到多个节点的PyQuery对象构造成一个生成器

## 遍历,通过PyQuery对象的items方法可以把匹配到多个节点的PyQuery对象构造成一个生成器
from pyquery import PyQuery as pq doc = pq(doc)
print(doc('a'))
items = doc('a').items()
print(type(items))
for i, item in enumerate(items):
print(i, item, type(item))
'''
输出内容:
<a href="https://www.baidu.com/1" class="error" id="l1"><span><!-- 1 --></span></a>,
<a href="https://www.baidu.com/2" class="error" id="l2"><span>2</span></a> and
<a href="https://www.baidu.com/3" class="error" id="l3">3</a>;
66666666666 <class 'generator'>
0 <a href="https://www.baidu.com/1" class="error" id="l1"><span><!-- 1 --></span></a>,
<class 'pyquery.pyquery.PyQuery'>
1 <a href="https://www.baidu.com/2" class="error" id="l2"><span>2</span></a> and
<class 'pyquery.pyquery.PyQuery'>
2 <a href="https://www.baidu.com/3" class="error" id="l3">3</a>;
66666666666
<class 'pyquery.pyquery.PyQuery'>
'''

输出内容

5. 获取属性和文本

## 获取属性和文本
from pyquery import PyQuery as pq doc = pq(doc)
# 获取属性,当多个节点时,同上,用items方法构造生成器然后进行遍历输出
a = doc('.error.ed2')
print(a, type(a))
print(a.attr('href'))
print(a.attr.href) '''
输出结果:
<a href="https://www.baidu.com/2" class="error ed2" id="l2"><span>2</span></a> and
<class 'pyquery.pyquery.PyQuery'>
https://www.baidu.com/2
https://www.baidu.com/2
''' ## 获取文本
from pyquery import PyQuery as pq doc = pq(text)
print(doc('a.error'))
# 第一个a节点文本内容为注释内容,所以不输出
print(doc('a.error').text()) # 只输出节点内的文本内容
print(doc('a.error').html()) # 输出节点内的内容,包含标签内容
items = doc('a.error').items()
for i, item in enumerate(items):
print(i, type(item), item.text())
print(i, type(item), item.html()) '''
输出内容:
<a href="https://www.baidu.com/1" class="error" id="l1"><span><!-- 1 --></span></a>,
<a href="https://www.baidu.com/2" class="error ed2" id="l2"><span>2</span></a> and
<a href="https://www.baidu.com/3" class="error" id="l3">3</a>;
66666666666 2 3
<span><!-- 1 --></span>
0 <class 'pyquery.pyquery.PyQuery'>
0 <class 'pyquery.pyquery.PyQuery'> <span><!-- 1 --></span>
1 <class 'pyquery.pyquery.PyQuery'> 2
1 <class 'pyquery.pyquery.PyQuery'> <span>2</span>
2 <class 'pyquery.pyquery.PyQuery'> 3
2 <class 'pyquery.pyquery.PyQuery'> 3
'''

6. 节点操作

## 节点操作
from pyquery import PyQuery as pq doc = pq(text)
p = doc('.title')

# addClass和removeClass,增加或减少class属性值
p.add_class('admin')
print(p.attr.class_)
p.removeClass('title')
print(p.attr('class'))

# attr、text和html,修改属性、文本、HTML文本内容
print(p.attr.name)
print(p.text())
print(p.html())
p.attr('name', 'test')
print(p.attr.name)
p.text('change text')
print(p.text())
p.html('<span>change html</span>')
print(p.html())

# remove,移除节点
doc = pq(text)
p = doc('.title')
print(p.html())
p.remove('b')
print(p.html())
'''
输出内容:
title admin
admin
dmr
there is money
<b>there is money</b>
test
change text
<span>change html</span>
<b>there is money</b>contents
contents
'''

输出内容

7. 伪类编辑器

# 伪类编辑器
from pyquery import PyQuery as pq doc = pq(text)
# 第一个a节点
a = doc('a:first-child')
print(a)
# 最后一个a节点
a = doc('a:last-child')
print(a)
# 第二个a节点
a = doc('a:nth-child(2)')
print(a)
# 第0个节点之后的节点
a = doc('a:gt(0)')
print(a)
# 偶数位置的节点
a = doc('a:nth-child(2n)')
print(a)
# 文本内容包含3的节点
a = doc('a:contains("3")')
print(a) '''
输出内容:
<a href="https://www.baidu.com/1" class="error" id="l1"><span><!-- 1 --></span></a>, <a href="https://www.baidu.com/3" class="error" id="l3">3</a>;
66666666666 <a href="https://www.baidu.com/2" class="error ed2" id="l2"><span>2</span></a> and <a href="https://www.baidu.com/2" class="error ed2" id="l2"><span>2</span></a> and
<a href="https://www.baidu.com/3" class="error" id="l3">3</a>;
66666666666 <a href="https://www.baidu.com/2" class="error ed2" id="l2"><span>2</span></a> and <a href="https://www.baidu.com/3" class="error" id="l3">3</a>;
66666666666
'''

pyquery解析库的介绍和使用的更多相关文章

  1. 【Python爬虫】PyQuery解析库

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

  2. Python3 BeautifulSoup和Pyquery解析库随笔

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

  3. BeautifulSoup解析库的介绍和使用

    ### BeautifulSoup解析库的介绍和使用 ### 三大选择器:节点选择器,方法选择器,CSS选择器 ### 使用建议:方法选择器 > CSS选择器 > 节点选择器 ## 测试文 ...

  4. 第四节:Web爬虫之pyquery解析库

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

  5. pyquery解析库

    这一篇整理一下pyquery这个解析库.还是菜,若有错误的地方,欢迎大家随时指正.......(come on.......) pyquery:是一个css选择器,再使用时,也需要传入HTML文本来初 ...

  6. Pyquery解析库的安装和使用

    Pyquery同样是一个强大的网页解析工具,它提供了和jQuery类似的语法来解析HTML文档,支持CSS选择器,使用非常方便.GitHub:https://github.com/gawel/pyqu ...

  7. xpath beautiful pyquery三种解析库

    这两天看了一下python常用的三种解析库,写篇随笔,整理一下思路.太菜了,若有错误的地方,欢迎大家随时指正.......(conme on.......) 爬取网页数据一般会经过 获取信息-> ...

  8. Python爬虫3大解析库使用导航

    1. Xpath解析库 2. BeautifulSoup解析库 3. PyQuery解析库

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

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

随机推荐

  1. 极简实用的Asp.NetCore框架再新增商城模块

    概述 关于这个框架的背景,在前面我已经交代过了.不清楚的可以查看这个链接 1.极简实用的Asp.NetCore模块化框架决定免费开源了 2.极简实用的Asp.NetCore模块化框架新增CMS模块 算 ...

  2. Linux基础是零基础必须要过的关,你懂了多少

    #LINUX基础学习 ##命令行下的基础知识 Linux区分英文的大小写. date :查看时间 cal:查看日历 [Tab] 热键 :可以自动补全命令名和文件名 [Ctrl]+C 热键 :可以中断正 ...

  3. Redis的浅入门

    Redis的浅入门 # 缓存的思想 问题提出:我们的用户数量上亿,如果登录,访问数据库user特别耗时,该怎么办?--提出缓存 方法:怎样从缓存在获取数据? *有数据: 直接返回 *无数据: (1)从 ...

  4. 从零开始的DIY智能家居 - 基于 ESP32 的土壤湿度传感器

    前言 自从上次做了那个 甲醛传感器 和 水浊度传感器 之后开始尝到智能家居的甜头了,这两东西有没有用我不知道,但是没事的时候掏出手机瞄两眼,看着就让人很安心( ̄︶ ̄). 于是懒惰的我开始琢磨把给植物浇 ...

  5. mdev 响应热插拔事件

    热插拔事件是从内核空间发送到用户空间的通知,一般这时候系统配置出现了变化,比如插入/移除耳机,插入/移除SD卡或者USB存储设备等. 热插拔事件默认会调用/sbin/hotplug来加载驱动程序,创建 ...

  6. P1231 教辅的组成(最大流)

    P1231 教辅的组成 这个题一看便知是网络流量,(三分图??滑稽..) 就一个小细节,如果我们仅仅将所有的点分成三部分跑网络流的话会有点小问题.. 因为这可能导致一本书被重复利用,就是有两条流经过同 ...

  7. shell 中的判断

    一.if的基本语法: if [ command ];then    符合该条件执行的语句 elif [ command ];then    符合该条件执行的语句 else    符合该条件执行的语句 ...

  8. K8s 离线集群部署(二进制包无dashboard)

    https://www.cnblogs.com/cocowool/p/install_k8s_offline.html https://www.jianshu.com/p/073577bdec98 h ...

  9. iNeuOS工业互联网操作系统,发布实时存储方式:实时存储、变化存储、定时存储,增加设备振动状态和电能状态监测驱动,v3.6.2

    目       录 1.      概述... 1 2.      平台演示... 2 3.      存储方式... 2 4.      设备状态和用电状态监控驱动... 3 1.   概述 本次升 ...

  10. Java测试开发--Set、Map、List三种集合(四)

    1.集合类型主要有3种:set(集).list(列表)和map(映射). 2.三者关系 3.Set set接口是Collection接口的一个子接口,是无序的,set去重,也就是说set中不存在两个这 ...