安装命令:
pip install pyquery
#coding=utf-8
from pyquery import PyQuery as pq
html='''
Hello
- Foo
- Bar
- Jay
- Foo
- Bar
'''
# doc 一个pyquery对象
doc = pq(html)
# 获取html中所有的 li 标签
print(doc('li'))
'''
Foo
Bar
Jay
Foo
Bar
'''
#coding=utf-8
from pyquery import PyQuery as pq
# doc 一个pyquery对象
doc = pq(url="https://search.51job.com/list/000000,000000,0000,00,9,99,%2B,2,3.html?lang=c&stype=1&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=99&companysize=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare="
,encoding='gbk')
# 获取html中所有的 li 标签
print(doc('title'))
'''
【全国招聘,求职】-前程无忧
'''
#coding=utf-8
from pyquery import PyQuery as pq
doc = pq(filename='demo.html')
print(doc('li'))
'''
first item
second item
third item
fourth item
fifth item
'''
# coding=utf-8
from pyquery import PyQuery as pq
html = '''
- first item
- second item
- third item
- fourth item
- fifth item
'''
doc = pq(html)
print(doc('#container .item-0.active a'))
'''
third item
'''
# coding=utf-8
from pyquery import PyQuery as pq
html = '''
- first item
- second item
- third item
- fourth item
- fifth item
fourth item
'''
doc = pq(html)
item = doc('#container .list')
print(item.find('li'))
print("----------------------------")
print(item.children('a'))
print("----------------------------")
print(item.children())
'''
first item
second item
third item
fourth item
fifth item
----------------------------
fourth item
----------------------------
first item
second item
third item
fourth item
fifth item
fourth item
'''
# coding=utf-8
from pyquery import PyQuery as pq
html = '''
- first item
- second item
- third item
- fourth item
- fifth item
fourth item
'''
doc = pq(html)
item = doc('#container .list .item-0')
print(item.parent())
print("----------------------------")
print(item.parents())
print("----------------------------")
print(item.parents('.list'))
'''
- first item
- second item
- third item
- fourth item
- fifth item
fourth item
----------------------------
- first item
- second item
- third item
- fourth item
- fifth item
fourth item
- first item
- second item
- third item
- fourth item
- fifth item
fourth item
----------------------------
- first item
- second item
- third item
- fourth item
- fifth item
fourth item
'''
# coding=utf-8
from pyquery import PyQuery as pq
html = '''
- first item
- second item
- third item
- fourth item
- fifth item
fourth item
'''
doc = pq(html)
item = doc('#container .list a')
print(item.siblings())
print("-------------------------------")
print(item.siblings('.item-0'))
'''
fifth item
fourth item
third item
second item
first item
-------------------------------
fifth item
third item
first item
'''
# coding=utf-8
from pyquery import PyQuery as pq
html = '''
- first item
- second item
- third item
- fourth item
- fifth item
fourth item
'''
doc = pq(html)
item = doc('#container .list li').items()
for one in item:
print(one)
'''
first item
second item
third item
fourth item
fifth item
'''
# coding=utf-8
from pyquery import PyQuery as pq
html = '''
- first item
- second item
- third item
- fourth item
- fifth item
fourth item
'''
doc = pq(html)
item = doc('#container .list .item-0')
a = item('a')
print(a.attr.href)
print(a.attr('href'))
print('----------------------------------')
print(a.text())
print('----------------------------------')
print(a.html())
'''
link3.html
link3.html
----------------------------------
third item fifth item
----------------------------------
third item
'''
# coding=utf-8
from pyquery import PyQuery as pq
html = '''
- first item
- second item
- third item
- fourth item
- fifth item
fourth item
'''
doc = pq(html)
li = doc('.item-0.active')
print(li.removeClass('active'))
print(li)
print("------------------------------")
print(li.addClass('sub'))
print(li)
'''
third item
third item
------------------------------
third item
third item
'''
# coding=utf-8
from pyquery import PyQuery as pq
html = '''
- first item
- second item
- third item
- fourth item
- fifth item
fourth item
'''
doc = pq(html)
a = doc('.list .item')
print(a.attr('id','test'))
print(a.remove_attr('id'))
print("------------------------------")
print(a.attr('class','test'))
print(a.remove_attr('class'))
'''
fourth item
fourth item
------------------------------
fourth item
fourth item
'''
# coding=utf-8
from pyquery import PyQuery as pq
html = '''
Hello, World
This is a paragraph.
'''
doc = pq(html)
div_text = doc('.sss')
print(div_text.text())
print("-------------------------")
print(div_text('p').remove())
print(div_text.text())
'''
Hello, World
This is a paragraph.
-------------------------
This is a paragraph.
Hello, World
'''
# coding=utf-8
from pyquery import PyQuery as pq
html = '''
- first item
- second item
- third item
- fourth item
- fifth item
'''
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
'''