本文主要是介绍007 PyQuery,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
-
- 安装PyQuery
- PyQuery
- 初始化
- 字符串初始化
- URL初始化
- 文件初始化
- 基本CSS选择器
- 查找元素
- 子元素
- 父元素
- 官方文档
- 初始化
1.安装PyQuery
pip install pyquery
2.PyQuery
1.初始化
1.字符串初始化
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'))
2.URL初始化
from pyquery import PyQuery as pq
doc = pq(url='http://www.baidu.com')
print(doc('head'))
3.文件初始化
from pyquery import PyQuery as pq
doc = pq(filename='demo.html')
print(doc('li'))
2.基本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'))
3.查找元素
1.子元素
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)
lis = items.children()
print(type(lis))
print(lis)
lis = items.children('.active')
print(lis)
2.父元素
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 = ”’
- first item
- second item
- third item
- fourth item
- fifth item
\”’
from pyquery import PyQuery as pq
doc = pq(html)
items = doc(‘.list’)
parents = items.parents()
print(type(parents))
print(parents)
#### 3.兄弟元素
html = ”’
- first item
- second item
- third item
- fourth item
- fifth item
\”’
from pyquery import PyQuery as pq
doc = pq(html)
li = doc(‘.list .item-0.active’)
print(li.siblings())
html = ”’
- first item
- second item
- third item
- fourth item
- fifth item
\”’
from pyquery import PyQuery as pq
doc = pq(html)
li = doc(‘.list .item-0.active’)
print(li.siblings(‘.active’))
### 4.遍历
#### 1.单个元素
html = ”’
- first item
- second item
- third item
- fourth item
- fifth item
\”’
from pyquery import PyQuery as pq
doc = pq(html)
li = doc(‘.item-0.active’)
print(li)
<li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
html = ”’
- first item
- second item
- third item
- fourth item
- fifth item
\”’
from pyquery import PyQuery as pq
doc = pq(html)
lis = doc(‘li’).items()
print(type(lis))
for li in lis:
print(li)
\<class 'generator'\>
\<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\>### 5.获取信息
#### 1.获取属性
html = ”’
- first item
- second item
- third item
- fourth item
- fifth item
\”’
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#### 2.获取文本
html = ”’
- first item
- second item
- third item
- fourth item
- fifth item
\”’
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#### 3.获取HTML
html = ”’
- first item
- second item
- third item
- fourth item
- fifth item
\”’
from pyquery import PyQuery as pq
doc = pq(html)
li = doc(‘.item-0.active’)
print(li)
print(li.html())
\<li class="item-0 active"\>\<a href="link3.html"\>\<span class="bold"\>third item\</span\>\</a\>\</li\>\<a href="link3.html"\>\<span class="bold"\>third item\</span\>\</a\>### 6.DOM操作
#### 1.addClass、removeClass
html = ”’
- first item
- second item
- third item
- fourth item
- fifth item
\”’
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\>#### 2.attr、css
html = ”’
- first item
- second item
- third item
- fourth item
- fifth item
\”’
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)
\<li class="item-0 active"\>\<a href="link3.html"\>\<span class="bold"\>third item\</span\>\</a\>\</li\>\<li class="item-0 active" name="link"\>\<a href="link3.html"\>\<span class="bold"\>third item\</span\>\</a\>\</li\>\<li class="item-0 active" name="link" style="font-size: 14px"\>\<a href="link3.html"\>\<span class="bold"\>third item\</span\>\</a\>\</li\>#### 3.remove
html = ”’
This is a paragraph.
\”’
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### 7.其他DOM方法
[pyquery文档]http://pyquery.readthedocs.io/en/latest/api.html### 8.伪类选择器
html = ”’
- first item
- second item
- third item
- fourth item
- fifth item
\”’
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)
“`
\
- first item\
\
- \fifth item\\
-
\
- second item\\
-
\
8.官方文档
[官方文档]http://pyquery.readthedocs.io/
这篇关于007 PyQuery的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!