0.安装:pip3 install pyquery

1.初始化

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'))

2.URL初始化

from pyquery import PyQuery as pq
doc = pq(url='http://www.baidu.com')
print(doc('head'))

3.文件初始化

from pyquery import PyQuery as pq
doc = pq(filename='demo.html')
print(doc('li'))

2.基本CSS选择器

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)
print(doc('#container .list li'))

3.查找元素

1.子元素

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) # 查询li标签
lis = items.find('li')
print(lis) # 查询孩子
lis = items.children()
print(type(lis))
print(lis) # 查询带有'.active'的孩子
lis = items.children('.active')
print(lis)

2.父元素

html = """
<div class='wrap'>
<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>
</div>
""" from pyquery import PyQuery as pq
doc = pq(html)
items = doc('.list')
print(items) parents = items.parents()
print(parents) parent = items.parents('.wrap')
print(parent)

3.兄弟元素

html = """
<div class='wrap'>
<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>
</div>
""" from pyquery import PyQuery as pq
doc = pq(html) items = doc('.list .item-0.active')
print(items)
print(items.siblings())
print(items.siblings('.active'))

4.遍历

html = """
<div class='wrap'>
<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>
</div>
""" from pyquery import PyQuery as pq
doc = pq(html)
# 单个元素
items = doc('.item-0.active')
print(items)
# 遍历元素
lis = doc('li').items()
print(lis)
# <generator object PyQuery.items at 0x0000000003A84468>
for item in lis:
print(item)

5.获取信息

1.获取属性

html = """
<div class='wrap'>
<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>
</div>
""" from pyquery import PyQuery as pq
doc = pq(html)
a = doc('.item-0.active a')
print(a)
print(a.attr('href'))
print(a.attr.href)

2.获取文本

html = """
<div class='wrap'>
<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>
</div>
""" from pyquery import PyQuery as pq
doc = pq(html) a = doc('.item-0.active a')
print(a)
print(a.text())

3.获取HTML

html = """
<div class='wrap'>
<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>
</div>
""" from pyquery import PyQuery as pq
doc = pq(html) li = doc('.item-0.active')
print(li)
print(li.html())

6.DOM操作

1.addclass、removeclass

html = """
<div class='wrap'>
<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>
</div>
""" from pyquery import PyQuery as pq
doc = pq(html) li = doc('.item-0.active')
print(li) li.removeClass('active')
print(li) li.addClass('active')
print(li)

2. attr、css

html = """
<div class='wrap'>
<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>
</div>
""" from pyquery import PyQuery as pq
doc = pq(html) li = doc('.item-0.active')
print(li) li.attr('name','apollo')
print(li) li.css('font-size','14px')
print(li)

3. remove

html = """
<div class='wrap'>
Hello World
<p>This is paragraph</p>
</div>
""" from pyquery import PyQuery as pq
doc = pq(html)
wrap = doc('.wrap')
print('移除前:',wrap.text())
wrap.find('p').remove()
print('移除后:',wrap.text())

4.其他DOM方法

https://pyquery.readthedocs.io/en/latest/api.html

7.伪类选择器


html = """
<div class='wrap'>
<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>
</div>
""" from pyquery import PyQuery as pq
doc = pq(html)
li = doc('li:first-child')
print(li)
li = doc('li:last-child')
print(li)
li = doc('li:nth-child(2)')
print(li)
li = doc('li:gt(2)')
print(li)
li = doc('li:nth-child(2n)')
print(li)
li = doc('li:contains(second)')
print(li)

官网

https://pyquery.readthedocs.io

PyQuery的基本使用详解的更多相关文章

  1. PyQuery详解

    1.What is Pyquery? 答:灵活强大的网页解析库 2.安装: pip3 install pyquery 3.基本使用 初始化操作: 前言:在介绍之前小伙伴们我们先来了解下CSS的基本语法 ...

  2. python爬虫知识点详解

    python爬虫知识点总结(一)库的安装 python爬虫知识点总结(二)爬虫的基本原理 python爬虫知识点总结(三)urllib库详解 python爬虫知识点总结(四)Requests库的基本使 ...

  3. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  4. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

  5. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

  6. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

  7. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  8. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  9. Git初探--笔记整理和Git命令详解

    几个重要的概念 首先先明确几个概念: WorkPlace : 工作区 Index: 暂存区 Repository: 本地仓库/版本库 Remote: 远程仓库 当在Remote(如Github)上面c ...

随机推荐

  1. RabbitMQ学习之:(一)初识、概念及心得

    因为网上有一篇很好的RMQ的入门帖子http://lostechies.com/derekgreer/tag/rabbitmq/,所以我就不多说了,就说说我目前看了该作者1~5章后,自己的心得.(所以 ...

  2. hdu 3836 Equivalent Sets(强连通分量--加边)

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

  3. Mtx——Mobile Tutorial Series (LibGDX & MTX)

    http://moribitotechx.blogspot.co.uk/p/tutorial-series-libgdx-mtx.html —————————————————————————————— ...

  4. 第二百零六节,jQuery EasyUI,Menu(菜单)组件

    jQuery EasyUI,Menu(菜单)组件 学习要点: 1.加载方式 2.菜单项属性 3.菜单属性 4.菜单事件 5.菜单方法 本节课重点了解 EasyUI 中 Menu(菜单)组件的使用方法, ...

  5. json DateTime转换

    前台: function ChangeDateFormat(jsondate) { jsondate = jsondate.replace("/Date(", "&quo ...

  6. java collection 类图

    转载:http://visionsky.blog.51cto.com/733317/371809/

  7. ActiveMQ安装优化

    ActiveMQ性能測试 http://m.blog.csdn.net/blog/brushli/41750615 1.下载ActiveMQ 官网:http://activemq.apache.org ...

  8. 转载 web前端简历

    Web前端程序员简历模板 本简历模板由国内首家互联网人才拍卖网站「 JobDeer.com 」提供. (括号里的是我们的顾问编写的说明,建议在简历书写完成后统一删除) 先讲讲怎样才是一份好的技术简历 ...

  9. iOS7中弹簧式列表的制作

    本文转载至 http://www.devdiv.com/forum.php?mod=viewthread&tid=208170&extra=page%3D1%26filter%3Dty ...

  10. 我的JavaScript笔记--面向对象

        单例模式 ??(基于对象,不能批量生产)  var person = {             name: "ywb",             sayHi: funct ...