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)


相关推荐

  • Centos7关闭selinux命令「建议收藏」

    关闭selinux步骤0x01用vi修改selinux的配置文件vi/etc/selinux/config0x02修改#SELINUX=enforcing为SELINUX=disabled这里就不修改了,注释掉好了,再直接复制修改为SELINUX=disabledselinux的工作模式enforcing强制模式permissive宽容模式disabled关闭什么是selinux?selinux是Linux的一种安全子系统 Linux中的权限管..

  • ansible 学习笔记

    ansible 学习笔记

  • JDBC_3 数据库事物

    JDBC_3 数据库事物数据库事务数据一旦提交,就不可回滚那些操作会导致数据的自动提交?DDL操作一旦执行,都会自动提交-. set autocommit = false不起作用DML默认情况下,一旦执行就会自动提交-. 可以设置set autocommit = false关闭连接的时候会自动提交 Connection connection = DriverManager.getConnection(url, user, password); connection.setAutoCommit

  • tomcat配置教程idea_idea2019配置tomcat

    tomcat配置教程idea_idea2019配置tomcat1.打开idea,在项目运行列表下拉选择“editConfigurations”2.在打开的界面,点击“+”,再选择下面的TomcatServer下的local3.在打开的界面,第一行“Name”中填入tomcat的名称然后点击Configure…,在ApplicationServers界面,点击“+”,在TomcatServer配置界面选择要添加的tomcat…

    2022年10月30日
  • linux内核编程_linux内核是什么

    linux内核编程_linux内核是什么什么是操作系统?指在系统中负责完成最基本功能和系统管理的部分,操作系统有哪些组成部分?内核——操作系统的内在核心 设备驱动程序 启动引导程序 命令行shell 其他种类的用户界面—-操作系统的外在表象 基本的文件管理工具和系统工具Linux内核的组成Linux内核源代码目录结构是什么,各目录有什么含义?arch:包含和硬件体系结构相关的代码,每种平台占一…

  • 验证码Kaptcha的使用「建议收藏」

    验证码Kaptcha的使用「建议收藏」Kaptcha是一个非常实用的验证码生成工具,可以通过配置生成多样化的验证码。以图片的形式显示,从而无法进行复制粘贴。

发表回复

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

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