html = '''
- first item
- second item
- third item
- fourth item
- fifth item
'''
from pyquery import PyQuery as pq
doc = pq(html)
print(doc('li')) # CSS选择器
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'))
ç™¾åº¦ä¸€ä¸‹ï¼Œä½ å°±çŸ¥é“
from pyquery import PyQuery as pq
doc = pq(filename='demo.html')
print(doc('li'))
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
in ()
1 from pyquery import PyQuery as pq
----> 2 doc = pq(filename='demo.html')
3 print(doc('li'))
D:\Anaconda3\lib\site-packages\pyquery\pyquery.py in __init__(self, *args, **kwargs)
214 # specific case to get the dom
215 if 'filename' in kwargs:
--> 216 html = open(kwargs['filename'])
217 elif 'url' in kwargs:
218 url = kwargs.pop('url')
FileNotFoundError: [Errno 2] No such file or directory: 'demo.html'
‘id’ = ‘#’, ‘class’ = ‘.’, 标签直接写
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')) # 'id' = '#', 'class' = '.'
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
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('.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(type(lis))
for li in lis:
print(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)
a = doc('.item-0.active a')
print(a)
print(a.attr('href'))
print(a.attr.href)
third item
link3.html
link3.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 a')
print(a)
print(a.text())
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)
print(li.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('active')
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 paragraph.
'''
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 paragraph.
Hello, World
http://pyquery.readthedocs.io/en/latest/api.html
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
更多CSS选择器可以查看
http://www.w3school.com.cn/css/index.asp
http://pyquery.readthedocs.io/