Python- 玩转数据-PyQuery库

Python- 玩转数据-PyQuery库

一、说明

pyquery库是jQuery的Python实现,能够以jQuery的语法来操作解析 HTML 文档,和XPATH,Beautiful Soup比起来,PyQuery更加灵活,提供增加节点的class信息,移除某个节点,提取文本信息等功能
使用lxml操作xml和html文档。

二、初始化PyQuery对象

html文档的所有操作都需要PyQuery对象来完成,初始化PyQuery对象主要有三种方式,分别是通过网址、字符串和文件名创建。

方式一:通过网址初始化PyQyery对象

from pyquery import PyQuery as pq

s = '<html><title>PyQuery用法总结<title></html>'
doc = pq(s)
print(doc('title'))

运行结果:

<title>PyQuery用法总结</title>

PyQuery还会将残缺的html文档补全。看下面的代码:

from pyquery import PyQuery as pq

s = '<html><title>PyQuery用法总结</title>'
doc = pq(s)
print(doc('html'))

运行的结果:

<html><head><title>PyQuery用法总结</title></head></html>

补全了html和head节点

方式二:URL网址初始化PyQyery对象

from pyquery import PyQuery as pq

url = 'http://www.bigdata17.com'
doc = pq(url=url,encoding='utf-8')
print(doc('title'))

运行结果:

<title>Home - Summer哥的自留地</title>

方式三:通过文件初始化PyQyery对象

这个方式也比较常用,很多时候我们会将网站爬取下来然后保存在本地磁盘:

from pyquery import PyQuery as pq

doc = pq(filename='test_pyquery.html',encoding='utf-8')
print(doc('title'))

三、访问节点属性:

使用attr()方法访问节点的属性:

from pyquery import PyQuery as pq
li = pq('<li id="test1" class="test1"></li><li id="test2" class="test2"></li>')('li')
print(li.attr("id"))

运行结果:

test1

上面的代码中有两个id不同的li节点,但是attr()方法只取第一个li节点的id属性值

from pyquery import PyQuery as pq
li = pq('<li class="test1"></li><li id="test2" class="test2"></li>')('li')
print(li.attr("id"))

运行结果:

None

第一个li节点没有id属性,因此返回结果为None,所以可见,attr()方法返回的是第一个节点的属性值。

多个li节点的属性值

结合items()方法来实现。items()方法是返回的节点的生成器generator object PyQuery.items:

from pyquery import PyQuery as pq
li = pq('<li id="test1" class="test1"></li><li id="test2" class="test2"></li>')('li')
print(li.items())
for item in li.items():
    print(item.attr("id"))

运行结果:

<generator object PyQuery.items at 0x0000027F26082728>
test1
test2

动态添加节点属性

PyQuery有很多方法动态添加节点的属性

addClass(),动态添加节点class属性:

from pyquery import PyQuery as pq
html = '<li id="test1" class="test1"></li>'
li = pq(html)('li')
li.addClass("addClass")
print(li)

运行结果:

<li id="test1" class="test1 addClass"/>

动态添加其他属性

attr()方法就可以实现:

from pyquery import PyQuery as pq

html = '<li id="test1" class="test1"></li>'
li = pq(html)('li')
li.attr("name","li name")
print(li)
li.attr("type","li")
print(li)
print(li.attr("type"))

运行结果:

<li id="test1" class="test1" name="li name"/>
<li id="test1" class="test1" name="li name" type="li"/>
li

attr()方法只有一个参数时,是获取节点的属性值,有两个参数时,是给节点添加属性及属性值,第一个参数时属性,第二个参数时属性值。

动态移除节点的class属性removeClass()

from pyquery import PyQuery as pq
html = '<li id="test1" class="test1"></li>'
li = pq(html)('li')
li.removeClass("test1")
print(li)

运行结果:

<li id="test1" class=""/>

动态添加/修改文本值

from pyquery import PyQuery as pq
html = '<li id="test1" class="test1"></li>'
li = pq(html)('li')
li.html("use html() dynamic add text")
print(li)
li.text("use text() dynamic add text")
print(li)

运行结果:

<li id="test1" class="test1">use html() dynamic add text</li>
<li id="test1" class="test1">use text() dynamic add text</li>

可见使用html()和text()方法都可以动态的给节点添加或修改节点的文本值。

获取节点文本值

PyQuery提供text()和html()方法获取节点的文本属性值

from pyquery import PyQuery as pq
html = '<li id = "test_id">li text value</li>'
li = pq(html)('li')
print(li.text())
print(li.html())

运行结果:

li text value
li text value

html()和text()如果没参数,则是获取属性的文本值,如果有参数,则是改变或者添加节点的属性值。

移除节点
动态移除节点remove():

from pyquery import PyQuery as pq
html = '''
<ul>
hello I am ul tag
<li>hello I am li tag</li>
</ul>
'''
ul = pq(html)('ul')
print(ul.text())
print('执行remove()移除节点')
ul.find('li').remove()
print(ul.text())

运行结果:

hello I am ul tag
hello I am li tag
执行remove()移除节点
hello I am ul tag

查找节点
PyQuery支持使用css的.和#来查找节点:

from pyquery import PyQuery as pq
html = '''
<div class="div_tag">
<ul id = "ul_tag">
hello I am ul tag
<li>hello I am li tag</li>
<li>hello I am li tag too</li>
</ul>
</div>
'''
doc = pq(html)
print(doc('.div_tag #ul_tag li'))

运行结果:

<li>hello I am li tag</li>
<li>hello I am li tag too</li>

上述代码是通过.div_tag获取class为div_tag的节点,然后通过#ul_tag获取id为ul_tag的节点,最后返回所有的li节点。

再看一例子:

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)

执行结果:

< li class ="item-0" > first item < / li >
< li class ="item-0" > < a href="link5.html" > fifth item < / a > < / li >
< li class ="item-1" > < a href="link2.html" > second item < / 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 >
< li class ="item-1" > < a href="link2.html" > second item < / a > < / li >
< li class ="item-1 active" > < a href="link4.html" > fourth item < / a > < / li >
< li class ="item-1" > < a href="link2.html" > second item < / a > < / li >

查找节点find()方法

html = '''
<div class="div_tag">
<ul id = "ul_tag">
hello I am ul tag
<li>hello I am li tag<a>www.bigdata17.com</li>
<li>hello I am li tag too</li>
</ul>
</div>
'''
doc = pq(html)
print(doc('.div_tag #ul_tag').find("li"))

运行结果:

<li>hello I am li tag<a>www.bigdata17.com</a></li>
<li>hello I am li tag too</li>

可见find(“li”)是把所有li节点及子节点都查找出来。

获取当前节点的所有子节点children()方法

该方法可以传入css选择器:children(’.ul_tag’)。

html = '''
<div class="div_tag">
<ul id = "ul_tag">
hello I am ul tag
<li>hello I am li tag<a>www.bigdata17.com</li>
<li>hello I am li tag too</li>
</ul>
</div>
'''
doc = pq(html)
print(doc('.div_tag #ul_tag').children())

运行结果:

<li>hello I am li tag<a>www.bigdata17.com</a></li>
<li>hello I am li tag too</li>

使用parent()方法获取当前节点的父亲节点:

html = '''
<div class="div_tag">
<ul id = "ul_tag">
hello I am ul tag
<li>hello I am li tag<a>www.bigdata17.com</li>
<li>hello I am li tag too</li>
</ul>
</div>
'''
doc = pq(html)
print(doc('.div_tag #ul_tag li').parent())

运行结果:

<ul id="ul_tag">
hello I am ul tag
<li>hello I am li tag<a>www.bigdata17.com</a></li>
<li>hello I am li tag too</li>
</ul>

上述代码通过.div_tag #ul_tag li css选择器定位到li节点,然后调用parent()方法获取li节点的父节点ul。

parents()返回当前节点的所有祖宗节点

html = '''
<div class="div_tag">
<ul id = "ul_tag">
hello I am ul tag
<li>hello I am li tag<a>www.bigdata17.com</li>
<li>hello I am li tag too</li>
</ul>
</div>
'''
doc = pq(html)
print(doc('.div_tag #ul_tag li').parents())

运行结果:

<html><body><div class="div_tag">
<ul id="ul_tag">
hello I am ul tag
<li>hello I am li tag<a>www.bigdata17.com</a></li>
<li>hello I am li tag too</li>
</ul>
</div>
</body></html><body><div class="div_tag">
<ul id="ul_tag">
hello I am ul tag
<li>hello I am li tag<a>www.bigdata17.com</a></li>
<li>hello I am li tag too</li>
</ul>
</div>
</body><div class="div_tag">
<ul id="ul_tag">
hello I am ul tag
<li>hello I am li tag<a>www.bigdata17.com</a></li>
<li>hello I am li tag too</li>
</ul>
</div>
<ul id="ul_tag">
hello I am ul tag
<li>hello I am li tag<a>www.bigdata17.com</a></li>
<li>hello I am li tag too</li>
</ul>

上面代码返回li节点的所有祖宗节点:html,body,div,ul。

siblings()方法返回当前节点的兄弟节点:

html = '''
<div class="div_tag">
<ul id = "ul_tag">
hello I am ul tag
<li class="li_class1">hello I am li tag<a>www.bigdata17.com</li>
<li class="li_class2">hello I am li tag too</li>
<li class="li_class3">hello I am the third li tag</li>
</ul>
</div>
'''
doc = pq(html)
print(doc('.div_tag #ul_tag .li_class1').siblings())

运行结果:

<li class="li_class2">hello I am li tag too</li>
<li class="li_class3">hello I am the third li tag</li>

使用.div_tag #ul_tag .li_class1 CSS节点选择器获取到class为liclassq1的li节点,就是第一个li节点,然后调用siblings()方法获取到子节点,分别是
第二和第三个li节点。

sibligs()还支持传入css选择器筛选符合条件的li节点:

print(doc('.div_tag #ul_tag .li_class1').siblings('.li_class3'))
1

运行结果:

<li class="li_class3">hello I am the third li tag</li>

四、遍历

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)
lis = doc('li').items()#.items会是一个生成器
print(type(lis))
for li in lis:
    print(li)
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值