大家好,又见面了,我是你们的朋友全栈君。
PyQuery
""" 强大又灵活的网页解析库 pip install pyquery 官方文档 http://pyquery.readthedocs.io/ API风格和前段jquery 相似 """
初始化
字符串初始化
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')) #传入css选择器,jQuery也是css选择器
# 选择id 就使用#
# 选择class 前面加.
# 标签名 什么也不加,直接传
URL初始化
# 自动请求url,把得到的html传递给函数
from pyquery import PyQuery as pq
doc = pq(url='http://www.baidu.com')
print(doc('head'))
文件初始化
from pyquery import PyQuery as pq
doc = pq(filename='demo.html')
print(doc('li'))
基本CSS选择器
""" 选择id 就使用# 选择class 前面加. 标签名 什么也不加,直接传 """
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'))
#这里我们需要注意的一个地方是doc('#container .list li'),这里的三者之间的并不是必须要挨着,只要是层级关系就可以,
常用CSS选择器方法
class | .color | 选择class=’color’内的所有元素 |
---|---|---|
#id | #info | 选择id=’info’的所有元素 |
* | * | 选择所有元素 |
element | p | 选择所有的p元素 |
element,element | div,p | 选择所有div和p元素 |
element element | div p | 选择所有div内的p元素 |
[attribute] | [target] | 选择所有带target属性的元素 |
[attribute=value] | [target=_blank] | 选择target=”_blank”的所有元素 |
查找元素
from pyquery import PyQuery as pq
doc = pq(html)
items = doc('.list')
print(type(items))
print(items)
lis = items.find('li') # 向里面查找li
print(type(lis))
print(lis)
# 查找子元素
li = items.children()
print(type(li))
print(li)
#父元素
from pyquery import PyQuery as pq
doc = pq(html)
items = doc('.list')
container = items.parent()
print(type(container))
print(container)
#祖先元素
from pyquery import PyQuery as pq
doc = pq(html)
items = doc('.list')
parents = items.parents()
print(type(parents))
print(parents)
# 兄弟元素
from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.list .item-0.active')
print(li.siblings())
获取信息
# 获取属性
""" pyquery对象.attr(属性名) pyquery对象.attr.属性名 """
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)
""" <a href="link3.html"><span class="bold">third item</span></a> link3.html link3.html """
# 获取文本
from pyquery import PyQuery as pq
doc = pq(html)
a = doc('.item-0.active a')
print(a)
print(a.text())
""" <a href="link3.html"><span class="bold">third item</span></a> third item """
# 获取html
from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.item-0.active')
print(li)
print(li.html())
DOM操作
# 添加和删除属性
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)
""" <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-0"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> """
# 同样的我们可以通过attr给标签添加和修改属性,
from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.item-0.active')
print(li)
li.attr('name', 'link')
print(li)
li.css('font-size', '14px')
print(li)
# remove
# 有时候我们获取文本信息的时候可能并列的会有一些其他标签干扰,这个时候通过remove就可以将无用的或者干扰的标签直接删除,从而方便操作
html = ''' <div class="wrap"> Hello, World <p>This is a 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())
""" Hello, World This is a paragraph. Hello, World 其他DOM方法 http://pyquery.readthedocs.io/en/latest/api.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('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> 更多CSS选择器可以查看 http://www.w3school.com.cn/css/index.asp """
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/137570.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...