"qqq"> <li class="1"><a data-moreurl-dict='{"from":"top-nav-click-main","uid":"0"}' href="https://www.douban.com" target="_blank">豆瓣</a></li> <li class="on off"><a data-moreurl-dict='{"from":"top-nav-click-book","uid":"0"}' href="https://book.douban.com">读书</a></li> <li class="2"><a data-moreurl-dict='{"from":"top-nav-click-movie","uid":"0"}' href="https://movie.douban.com" target="_blank">电影</a></li> </ul> '''

初始化

doc = pq(data)
print(doc('li'))
url初始化
doc = pq(url='http://www.baidu.com')
print(doc('head'))
文件初始化
doc = pq(filename='demo.html')
print(doc('head'))

基本CSS选择器

doc = pq(data)
items = doc('.qqq')
print(items)
print('-------------')
a = items.find('li')
print(a)

获取兄弟元素(不包括自己)

doc = pq(data)
li = doc.find('.on.off')
print(li.siblings())
doc = pq(data)
获取父元素
li = doc.find('.on')
print(li.parent())

遍历

doc = pq(data)
li = doc('li').items()
for i in li:
    print(i)
    print('---')
doc = pq(data)
a = doc('.on.off a')
print(a)

获取属性

print(a.attr('href'))
print(a.attr.href)

获取文本

print(a.text())

获取html

b = doc('.on.off')
print(b)
print(b.html())

dom操作

doc = pq(data)
li = doc('.on.off')
print(li)
li.remove_class('on')
print(li)
li.add_class('WWW')
print(li)
li.attr('name','linkkk')
print(li)
li.css('font-size','14px')
print(li)

remove

html = '''
<div class="wrap">
    Hello world
    <p>hahahahahahaahhahaha</p>
</div>
'''
doc = pq(html)
wrap = doc('.wrap')
print(wrap.text())
wrap.find('p').remove()
print(wrap.text())
相关标签: pyquery

上一篇: Keras 多线程下运行的问题

下一篇: PyQuery库的简单实用(Python)

推荐阅读