frompyquery importPyQuery aspy
html = '''
first item
second item
third item
fourth item
fifth item
'''
doc = py(html)
print(doc('li'))
1.2、URL初始化
frompyquery importPyQuery aspy
# 通過(guò)URL來(lái)獲取
doc = py(url='http://www.baidu.com')
#
print(type(doc('title')))
# 輸出選中的head標(biāo)簽
print(doc('head'))
1.3、文件的初始化
frompyquery importPyQuery aspy
# 通過(guò)文件來(lái)獲取
doc = py(filename='demo1.html')
#
print(type(doc('li')))
# 輸出所有的li標(biāo)簽
print(doc('li'))
2、基本的CSS選擇器
frompyquery importPyQuery aspy
html = '''
first item
second item
third item
fourth item
fifth item
'''
doc = py(html)
# 選中id為container中的class為list中的li標(biāo)簽
print(doc('#container .list li'))
3、查找元素
3.1、子元素
frompyquery importPyQuery aspy
html = '''
first item
second item
third item
fourth item
fifth item
'''
doc = py(html)
# 獲取class為list的元素
items = doc('.list')
#
print(type(items))
print(items)
# 在先前找到的元素中獲取li標(biāo)簽
lis = items.find('li')
#
print(type(lis))
print(lis)
# 獲取先前找到的元素中的所有子元素
lis2 = items.children()
print(type(lis2))
print(lis2)
# 獲取先前找到的元素中的class為active的元素
li3 = items.children('.active')
print(li3)
3.2、父元素
frompyquery importPyQuery aspy
html = '''
first item
second item
third item
fourth item
fifth item
'''
doc = py(html)
# 獲取class為list的元素
items = doc('.list')
# 獲取所選元素的父元素
container = items.parent()
print(type(container))
print(container)
print('==========================')
# 獲取所選元素的所有父元素
parents = items.parents()
print(type(parents))
print(parents)
print('==========================')
# 獲取所選元素的所有父元素中class為container的元素
parent = items.parents('.container')
print(parent)
3.3、兄弟元素
frompyquery importPyQuery aspy
html = '''
first item
second item
third item
fourth item
fifth item
'''
doc = py(html)
# 獲取class為list的元素
items = doc('.list')
li = doc('.list .item-0.active')
# 查找選中元素的所有兄弟元素(不包含自己)
print(li.siblings())
# 查找選中元素的所有兄弟元素中class為active的元素(不包含自己)
print(li.siblings('.active'))
4、遍歷
4.1、單個(gè)元素
frompyquery importPyQuery aspy
html = '''
first item
second item
third item
fourth item
fifth item
'''
doc = py(html)
# 選中單個(gè)單個(gè)元素
li = doc('.item-0.active')
print(li)
4.2、多個(gè)元素
frompyquery importPyQuery aspy
html = '''
first item
second item
third item
fourth item
fifth item
'''
doc = py(html)
# 查找所有li標(biāo)簽
lis = doc('li').items()
#
print(type(lis))
forli inlis:
print(li)
5、獲取信息
5.1、獲取屬性
frompyquery importPyQuery aspy
html = '''
first item
second item
third item
fourth item
fifth item
'''
doc = py(html)
a = doc('.item-0.active a')
# third item
print(a)
# link3.html 獲取選中標(biāo)簽的href屬性
print(a.attr('href'))
# link3.html
print(a.attr.href)
5.2、獲取文本
frompyquery importPyQuery aspy
html = '''
first item
second item
third item
fourth item
fifth item
'''
doc = py(html)
a = doc('.item-0.active a')
# third item
print(a)
# 獲取a標(biāo)簽的內(nèi)容
print(a.text())
5.3、獲取HTML
frompyquery importPyQuery aspy
html = '''
first item
second item
third item
fourth item
fifth item
'''
doc = py(html)
li = doc('.item-1.active')
# fourth item
print(li)
# 獲取li標(biāo)簽的HTML
print(li.html())
6、DOM操作
6.1、addClass、removeClass
frompyquery importPyQuery aspy
html = '''
first item
second item
third item
fourth item
fifth item
'''
doc = py(html)
li = doc('.item-0.active')
print(li)
# 移除class
li.removeClass('active')
print(li)
# 添加class
li.addClass('active')
print(li)
6.2、attr、css
frompyquery importPyQuery aspy
html = '''
first item
second item
third item
fourth item
fifth item
'''
doc = py(html)
li = doc('.item-0.active')
print(li)
# 添加name屬性
li.attr('name', 'link')
print(li)
# 添加css樣式
li.css('font-size', '14px')
print(li)
6.3、remove
frompyquery importPyQuery aspy
html = '''
Hello, World
This is a paragraph.
'''
doc = py(html)
wrap = doc('.wrap')
print(wrap.text())
# 在選擇的元素中找到p標(biāo)簽并移除
wrap.find('p').remove()
print(wrap.text())
6.4、其他DOM方法
http://pyquery./en/latest/api.html
7、偽類(lèi)選擇器
frompyquery importPyQuery aspy
html = '''
first item
second item
third item
fourth item
fifth item
'''
doc = py(html)
# 找到第一個(gè)li
li = doc('li:first-child')
print(li)
# 找到最后一個(gè)li
li = doc('li:last-child')
print(li)
# 找到第二個(gè)li
li = doc('li:nth-child(2)')
print(li)
# 找到第三個(gè)到最后的li
li = doc('li:gt(2)')
print(li)
# 找到第偶數(shù)個(gè)li
li = doc('li:nth-child(2n)')
print(li)
# 找到內(nèi)容包含second的li
li = doc('li:contains(second)')
print(li)
更多的選擇器
http://www.w3school.com.cn/css/index.asp
8、官方文檔
http://pyquery./