from pyquery import PyQuery as py
html = '''
'''
doc = py(html)
print(doc('li'))
from pyquery import PyQuery as py
# 通过URL来获取
doc = py(url='http://www.baidu.com')
#
print(type(doc('title')))
# 输出选中的head标签
print(doc('head'))
from pyquery import PyQuery as py
# 通过文件来获取
doc = py(filename='demo1.html')
#
print(type(doc('li')))
# 输出所有的li标签
print(doc('li'))
from pyquery import PyQuery as py
html = '''
'''
doc = py(html)
# 选中id为container中的class为list中的li标签
print(doc('#container .list li'))
from pyquery import PyQuery as py
html = '''
'''
doc = py(html)
# 获取class为list的元素
items = doc('.list')
#
print(type(items))
print(items)
# 在先前找到的元素中获取li标签
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)
from pyquery import PyQuery as py
html = '''
'''
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)
from pyquery import PyQuery as py
html = '''
'''
doc = py(html)
# 获取class为list的元素
items = doc('.list')
li = doc('.list .item-0.active')
# 查找选中元素的所有兄弟元素(不包含自己)
print(li.siblings())
# 查找选中元素的所有兄弟元素中class为active的元素(不包含自己)
print(li.siblings('.active'))
from pyquery import PyQuery as py
html = '''
'''
doc = py(html)
# 选中单个单个元素
li = doc('.item-0.active')
print(li)
from pyquery import PyQuery as py
html = '''
'''
doc = py(html)
# 查找所有li标签
lis = doc('li').items()
#
print(type(lis))
forli inlis:
print(li)
from pyquery import PyQuery as py
html = '''
'''
doc = py(html)
a = doc('.item-0.active a')
# third item
print(a)
# link3.html 获取选中标签的href属性
print(a.attr('href'))
# link3.html
print(a.attr.href)
from pyquery import PyQuery as py
html = '''
'''
doc = py(html)
a = doc('.item-0.active a')
# third item
print(a)
# 获取a标签的内容
print(a.text())
from pyquery import PyQuery as py
html = '''
'''
doc = py(html)
li = doc('.item-1.active')
# fourth item
print(li)
# 获取li标签的HTML
print(li.html())
from pyquery import PyQuery as py
html = '''
'''
doc = py(html)
li = doc('.item-0.active')
print(li)
# 移除class
li.removeClass('active')
print(li)
# 添加class
li.addClass('active')
print(li)
from pyquery import PyQuery as py
html = '''
'''
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)
from pyquery import PyQuery as py
html = '''
Hello, World
This is a paragraph.
'''
doc = py(html)
wrap = doc('.wrap')
print(wrap.text())
# 在选择的元素中找到p标签并移除
wrap.find('p').remove()
print(wrap.text())
http://pyquery.readthedocs.io/en/latest/api.html
from pyquery import PyQuery as py
html = '''
'''
doc = py(html)
# 找到第一个li
li = doc('li:first-child')
print(li)
# 找到最后一个li
li = doc('li:last-child')
print(li)
# 找到第二个li
li = doc('li:nth-child(2)')
print(li)
# 找到第三个到最后的li
li = doc('li:gt(2)')
print(li)
# 找到第偶数个li
li = doc('li:nth-child(2n)')
print(li)
# 找到内容包含second的li
li = doc('li:contains(second)')
print(li)
更多的选择器
http://www.w3school.com.cn/css/index.asp
http://pyquery.readthedocs.io/