- 安装
pip3 install pyquery
- Html初始化
from pyquery import PyQuery as pq
s = '<html><title>PyQuery Html初始化<title></html>'
doc = pq(s)
print(doc('title'))
- URL网址初始化
from pyquery import PyQuery as pq
url = 'https://www.baidu.com/'
doc = pq(url=url,encoding='utf-8')
print(doc('title'))
- 通过文件初始化
from pyquery import PyQuery as pq
doc = pq(filename='test_pyquery.html',encoding='utf-8')
print(doc('title'))
- pyquery的CSS选择器(Selectors)基本使用
CSS选择器 | 简易实例 | 说明[doc=pq(html) #html来自第1点:准备及初始化] |
---|---|---|
* | * | 选择所有的元素:doc(’*’) |
element | p | 选择所有的 元素:doc(‘p’) |
.class | .pidg | 选择所有class=’pidg’ 的元素:doc(’.pidg’) |
.class | p.pidg | 选择 class=’pidg’的<p>标签:doc(‘p.pidg’) |
#id | #name | 选择所有id="name"的元素:doc(’#name’) |
#id | a#name | 选择 id= ‘name’ 的<p>标签:doc(‘p#name’) |
element,element | p,a | 选择所有的 和元素:doc(‘p,a’) |
elment element | div p | 选择所有的div元素下所有的 元素:doc(‘div p’) #中间用空格 |
elment>element | div>td | 选择父元素为
元素的所有 td 元素:doc(‘div>td’)
|
elment+element | p+td | 选择紧接在 元素之后的所有 td 元素:doc(‘p+td’) #同级 |
element~element | td~td | 选择前面有 td 元素的每个 td 元素:doc(‘td~td’).text() #18 - 4 胜 6 119.5 理解:前面一个 td 元素的text() 是6,没有输出哦!!! |
[attribute] | [href] | 选择带有href属性的所有元素:doc("[href]") |
[attribute=value] | [href=bucks] | 选择href=bucks的所有元素:doc("[href=bucks]") /doc(’[href=“bucks”]’) |
[attribute=value] | a[href=“bucks”] | 选择元素属性href="bucks"的元素:doc(‘a[href=“bucks”]’) |
[attribute=value | [class=“nobr player desktop”] | 选择class="nobr player desktop"的所有元素: doc(’[class *=“nobr player desktop”]’) |
[attribute~=value] | [class~=desktop] | 选择class属性包含字符串desktop的所有元素:doc("[class~=desktop]") |
[attribute | =value] | [href |
[attribute^=value] | a[href ^= bu] | 选择href属性值以"bu"开头的每个元素:doc(‘a[href ^= bu]’) |
[attribute$=value] | a[href $=cks] | 选择href属性值以"bu"结尾的每个元素:doc(‘a[href $=cks]’) |
[attribute*=value] | [class*=desktop] | 选择class属性包含字符串desktop的所有元素:doc("[class*=desktop]") |
- 伪类选择器
方法 | 说明 | 案例[doc=pq(html) #html来自第1点:准备及初始化] |
---|---|---|
:first-child | 获取第一个节点 | doc(“tr>td:first-child”) |
:last-child | 获取最后一个节点 | doc(“tr>td:last-child”) |
:nth-child(N) | 获取第N个节点,N=1,2,… | doc(“tr>td:nth-child(2)”) |
:nth-child(2n) | 获取偶数位置的全部节点 | doc(“tr>td:nth-child(2n)”) |
:nth-child(2n-1) | 获取奇数位置的全部节点 | doc(“tr>td:nth-child(2n-1)”) |
:gt(N) | 获取索引大于N的节点,N=0,1,… | doc(“tr>td:gt(1)”) |
:contains(‘雄鹿’) | 获取文本包含"雄鹿"的节点 | doc(“td:contains(‘雄鹿’)”) |
- 遍历、获取信息(属性、文本)
方法 | 说明 | 案例[doc=pq(html) #html来自第1点:准备及初始化] |
---|---|---|
.items() | 遍历多节点 | for td in doc(‘tr>td’).items(): print(td) |
.attr() | 获取属性 | doc(“a”).attr(“href”) |
.attr. | 获取属性 | doc(“a”).attr.href |
.text() | 获取文本 | doc(“a”).text() #密尔沃基 雄鹿nba |
.html() | 获取节点内部的HTML文本 | doc(“a”).html() # 密尔沃基 雄鹿nba |
- 子(孙)节点,父(祖)节点查找、兄弟节点的查找
方法 | 说明 | 案例[doc=pq(html) #html来自第1点:准备及初始化] |
---|---|---|
.find() | 查找符合条件的所有子孙节点 | doc(‘div’).find(‘td’) |
.children() | 查找直接子节点 | doc(‘td[class=“nobr player desktop”]’).children() |
.children() | 查找符合条件的直接子节点 | doc(‘td[class=“nobr player desktop”]’).children(‘a[href=“bucks”]’) |
.parent() | 查找直接父节点 | doc(‘a[href=“bucks”]’).parent() |
.parent() | 查找符合条件的父节点 | doc(‘a[href=“bucks”]’).parent(‘td[class=“nobr player desktop”]’) |
.parents() | 查找祖先节点 | doc(‘a[href=“bucks”]’).parents() |
.parents() | 查找符合条件的祖先节点 | doc(‘a[href=“bucks”]’).parents(‘td[class=“nobr player desktop”]’) |
.siblings() | 查找全部兄弟标签 | doc(‘td[href=“href01”]’).siblings() |
.siblings() | 查找符合条件的兄弟标签 | doc(‘td[href=“href01”]’).siblings(‘td[class *= “nobr”]’) |
- 节点操作
方法 | 说明 | 案例[doc=pq(html) #html来自第1点:准备及初始化] |
---|---|---|
removeClass() | 移除class属性 | r=doc(“tr>td”).removeClass(“center”) 或者r=doc(“tr>td”).remove_class(“center”) |
addClass() | 添加class属性 | r=doc(“tr>td”).addClass(“nba”) r=doc(“tr>td”).add_class(“nba”) |
attr() | 添加属性a,值为nba | r=doc(“tr>td”).attr(“a”,“nba”) |
text() | 修改节点内文本为nba | r=doc(“td>a”).text(“nba”) |
html() | 修改节点内文本为nba | r=doc(“td>a”).html(“nba”) |
remove() | 移除指定节点 | doc(“tr”).remove() print(doc) |
- 具体python操作
https://blog.csdn.net/sinat_38682860/article/details/100165446