PyQuery

PyQueryPython崔老师爬虫课程听课笔记。

大家好,又见面了,我是你们的朋友全栈君。

PyQuery 崔老师爬虫系列课程学习笔记

安装

pip install pyquery

初始化方法

字符串初始化

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

URL初始化

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选择器

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

查找元素

子元素

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(type(items))
print(items)
lis = items.find('li')
print(type(lis))
print(lis)
list = items.children()
print(type(list))
print(list)
list = items.children('.active')
print(list)

父元素

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')
container = items.parent()
print(type(container))
print(container)
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')
parents = items.parents()#可以在括号里添加CSS选择标志
print(type(parents))
print(parents)

兄弟元素

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 .item-0.activate)#出现空格表示选择内部的条目,,连续无空格表示并列选择关系
print(items.siblings())#获取兄弟元素

遍历

单个元素

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)
item = doc('.item-0.active')
print(item)
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)
lis = doc('li').items()
print(type(lis))
for li in lis:
    print(li)

获取信息

获取属性

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)
a = doc('.item-1.active a')
print(a)
print(a.attr.href)
print(a.attr('href'))

获取文本

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)
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 = '<html><head><meta charset=utf-8><title>浙大美女校花 甜美神似张子萱</title><meta http-equiv=x-dns-prefetch-control content=on>'
from pyquery import PyQuery as pq
doc = pq(html)
a = doc('title')
print(a.text())
浙大美女校花 甜美神似张子萱

获取HTML

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)
a = doc('.item-0.active')
print(a.html())

DOM操作

addClass、removeClass

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 = doc('.item-1.active')#选中这一标签
print(li)

li.removeClass('active')
print(li)
li.addClass('active')
print(li)

attr、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)
li = doc('.item-0.active')
print(li)
li.attr('name','link')
print(li)
li.css('font-size','14px')
print(li)

remove挺重要

html = ''' <div class="wrap"> Hello, Word! <p> This is a paragraph.</p> </div> '''

from pyquery import PyQuery as pq
doc = pq(html)
a = doc('.wrap')
print(a.text())
a.find('p').remove()
print(a.text())

其他DOM方法

伪类选择器

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 = 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)')#获取第二个以后的,计数从0开始
print(li)
li = doc('li:nth-child(2n)')#获取偶数的标签
print(li)
li = doc('li:contains(second)')#获取包含“second”的
print(li)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/130338.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号