强大又灵活的网页解析库。如果你觉得正则写起来太麻烦,如果你觉得BeautifulSoup语法太难记,如果你熟悉jQuery的语法,那么PyQuery就是你的绝佳选择。
html = """
- first item
- second item
- third item
- fourth item
- fifth item
"""
from pyquery import PyQuery as pq
doc = pq(html)
print(doc('li'))
first item
second item
third item
fourth item
fifth item
from pyquery import PyQuery as pq
doc = pq(url='http://www.baidu.com')
print(doc('head'))
print(type(doc('head')))
ç™¾åº¦ä¸€ä¸‹ï¼Œä½ å°±çŸ¥é“
from pyquery import PyQuery as pq
doc = pa(filename='xxx')
print(doc('li'))
如果是类 .
如果是id #
如果是标签 就什么也不加
说白了 和CSS 选择器用法一样 也可以使用后代选择器...
html = """
- first item
- second item
- third item
- fourth item
- fifth item
"""
from pyquery import PyQuery as pq
doc = pq(html)
print(doc('#container .list li'))
first item
second item
third item
fourth item
fifth item
html = """
- first item
- second item
- third item
- fourth item
- fifth item
"""
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)
- first item
- second item
- third item
- fourth item
- fifth item
first item
second item
third item
fourth item
fifth item
lis = items.children()
print(type(lis))
print(lis)
first item
second item
third item
fourth item
fifth item
lis = items.children('.active')
print(lis)
third item
fourth item
html = """
- first item
- second item
- third item
- fourth item
- fifth item
"""
from pyquery import PyQuery as pq
doc = pq(html)
items = doc('.list')
container = items.parent()
print(type(container))
print(container)
- first item
- second item
- third item
- fourth item
- fifth item
# 查找所有的祖先元素
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)
- first item
- second item
- third item
- fourth item
- fifth item
- first item
- second item
- third item
- fourth item
- fifth item
parent = items.parents('.wrap')
print(parent)
- first item
- second item
- third item
- fourth item
- fifth item
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())
second item
first item
fourth item
fifth item
print(li.siblings('.active'))
fourth item
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)
third item
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(lis)
print(type(lis))
for li in lis:
print(li.find('a'))
second item
third item
fourth item
fifth item
通过attr(‘属性名’) 或 attr.属性名
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)
third item
link3.html
link3.html
通过 .text()方法获取文本内容
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())
third item
third item
通过.html()方式获取HTML代码
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')
print(a)
print(a.html())
third item
third item
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('acitve')
print(li)
third item
third item
third item
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)
third item
third item
third item
html = """
Hello,World
This is a paragrap
"""
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 paragrap
Hello,World
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
fourth item
fifth item
second item
fourth item
second item