Python中的jquery PyQuery庫使用小結(jié)
pyquery庫是jQuery的Python實(shí)現(xiàn),可以用于解析HTML網(wǎng)頁內(nèi)容,使用方法:
from pyquery import PyQuery as pq
1、可加載一段HTML字符串,或一個(gè)HTML文件,或是一個(gè)url地址,例:
d = pq("<html><title>hello</title></html>")
d = pq(filename=path_to_html_file)
d = pq(url='http://www.baidu.com') # 此處url必須寫全
2、html() 和 text() ——獲取相應(yīng)的HTML塊或文本塊,例:
p = pq("<head><title>hello</title></head>")
p('head').html() # 返回<title>hello</title>
p('head').text() # 返回hello
3、根據(jù)HTML標(biāo)簽來獲取元素,例:
d = pq('<div><p>test 1</p><p>test 2</p></div>')
d('p') # 返回[<p>,<p>]
print d('p') # 返回<p>test 1</p><p>test 2</p>
print d('p').html() # 返回test 1
注意:當(dāng)獲取到的元素不只一個(gè)時(shí),html()、text()方法只返回首個(gè)元素的相應(yīng)內(nèi)容塊
4、eq(index) ——根據(jù)給定的索引號(hào)得到指定元素
接上例,若想得到第二個(gè)p標(biāo)簽內(nèi)的內(nèi)容,則可以:
print d('p').eq(1).html() # 返回test 2
5、filter() ——根據(jù)類名、id名得到指定元素,例:
d = pq("<div><p id='1'>test 1</p><p class='2'>test 2</p></div>")
d('p').filter('#1') # 返回[<p#1>]
d('p').filter('.2') # 返回[<p.2>]
6、find() ——查找嵌套元素,例:
d = pq("<div><p id='1'>test 1</p><p class='2'>test 2</p></div>")
d('div').find('p') # 返回[<p#1>, <p.2>]
d('div').find('p').eq(0) #返回[<p#1>]
7、直接根據(jù)類名、id名獲取元素,例:
d = pq("<div><p id='1'>test 1</p><p class='2'>test 2</p></div>")
d('#1').html() # 返回test 1
d('.2').html() # 返回test 2
8、獲取屬性值,例:
d = pq("<p id='my_id'><a ) # 返回http://hello.com
d('p').attr('id') # 返回my_id
9、修改屬性值,例:
d('a').attr('href', 'http://baidu.com')
10、addClass(value) ——為元素添加類,例:
d = pq('<div></div>')
d.addClass('my_class') # 返回[<div.my_class>]
11、hasClass(name) #返回判斷元素是否包含給定的類,例:
d = pq("<div class='my_class'></div>")
d.hasClass('my_class') # 返回True
12、children(selector=None) ——獲取子元素,例:
d = pq("<span><p id='1'>hello</p><p id='2'>world</p></span>")
d.children() # 返回[<p#1>, <p#2>]
d.children('#2') # 返回[<p#2>]
13、parents(selector=None)——獲取父元素,例:
d = pq("<span><p id='1'>hello</p><p id='2'>world</p></span>")
d('p').parents() # 返回[<span>]
d('#1').parents('span') # 返回[<span>]
d('#1').parents('p') # 返回[]
14、clone() ——返回一個(gè)節(jié)點(diǎn)的拷貝
15、empty() ——移除節(jié)點(diǎn)內(nèi)容
16、nextAll(selector=None) ——返回后面全部的元素塊,例:
d = pq("<p id='1'>hello</p><p id='2'>world</p><img scr='' />")
d('p:first').nextAll() # 返回[<p#2>, <img>]
d('p:last').nextAll() # 返回[<img>]
17、not_(selector) ——返回不匹配選擇器的元素,例:
d = pq("<p id='1'>test 1</p><p id='2'>test 2</p>")
d('p').not_('#2') # 返回[<p#1>]
更多內(nèi)容,參考官網(wǎng) http://packages.python.org/pyquery
相關(guān)文章
解決python報(bào)錯(cuò):AttributeError:?'ImageDraw'?object?h
這篇文章主要給大家介紹了關(guān)于解決python報(bào)錯(cuò):AttributeError:?'ImageDraw'?object?has?no?attribute?'textbbox'的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01python下實(shí)現(xiàn)二叉堆以及堆排序的示例
下面小編就為大家?guī)硪黄猵ython下實(shí)現(xiàn)二叉堆以及堆排序的示例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09Python基于滑動(dòng)平均思想實(shí)現(xiàn)缺失數(shù)據(jù)填充的方法
今天小編就為大家分享一篇關(guān)于Python基于滑動(dòng)平均思想實(shí)現(xiàn)缺失數(shù)據(jù)填充的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02python在命令行中使用?pdb?實(shí)現(xiàn)斷點(diǎn)調(diào)試功能
在命令行中設(shè)置斷點(diǎn)通常需要使用調(diào)試工具來實(shí)現(xiàn),下面以 Python 為例介紹如何在命令行中使用pdb實(shí)現(xiàn)斷點(diǎn)調(diào)試,這篇文章主要介紹了python在命令行中使用pdb實(shí)現(xiàn)斷點(diǎn)調(diào)試,需要的朋友可以參考下2023-06-06對(duì)python3 中方法各種參數(shù)和返回值詳解
今天小編就為大家分享一篇對(duì)python3 中方法各種參數(shù)和返回值詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12Python-docx 實(shí)現(xiàn)整體修改或者部分修改文字的大小和字體類型
這篇文章主要介紹了Python-docx 實(shí)現(xiàn)整體修改或者部分修改文字的大小和字體類型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03