PyQuery 是仿照 jQuery 实现的,语法与 jQuery 几乎完全相同,如果你熟悉 jQuery,又不想再记一套 BeautifulSoup (Python 爬虫(三):BeautifulSoup 库) 的调用方法,那么 PyQuery 是一个很好的选择。
使用如下终端命令安装
pip install pyquery
安装完成后导包
from pyquery import PyQuery as pq
传入字符串
from pyquery import PyQuery as pq html = '''<html> <head> <title>Hello PyQuery</title> </head> <body> <ul id="container"> <li class="l1">l1</li> <li class="l2">l2</li> <li class="l3">l3</li> </ul> </body></html>'''doc = pq(html)print(type(doc))print(doc)
传入文件
from pyquery import PyQuery as pq doc= pq(filename='p.html')print(type(doc))print(doc)
传入 lxml.etree
from pyquery import PyQuery as pqfrom lxml import etree doc = pq(etree.fromstring('<html><title>Hello PyQuery</title></html>'))print(type(doc))print(doc)
传入 URL
from pyquery import PyQuery as pqdoc = pq('http://www.baidu.com')print(type(doc))print(doc)
获取元素
首先,我们使用 PyQuery 的 CSS 选择器获取指定元素。示例如下:
from pyquery import PyQuery as pq html = '''<html> <head> <title>Hello PyQuery</title> </head> <body> <ul id="container"> <li class="l1">l1</li> <li class="l2">l2</li> <li class="l3">l3</li> </ul> </body></html>'''doc = pq(html)# 获取 ulul = doc('#container')# 获取 lili = doc('ul li')print(ul)print(li)
遍历元素
from pyquery import PyQuery as pq html = '''<html> <head> <title>Hello PyQuery</title> </head> <body> <ul id="container"> <li class="l1">l1</li> <li class="l2">l2</li> <li class="l3">l3</li> </ul> </body></html>'''doc = pq(html)# 遍历 lilis =doc('li').items()for li in lis: print(li)
存在多个相同元素时,获取指定元素
from pyquery import PyQuery as pq html = '''<html> <head> <title>Hello PyQuery</title> </head> <body> <ul id="container"> <li class="l1">l1</li> <li class="l2">l2</li> <li class="l3">l3</li> </ul> </body></html>'''doc = pq(html)lis =doc('li').items()# 获取第二个 lil2 = list(lis)[1]print(l2)
获取父、子、兄弟元素
PyQuery 可以通过方法直接获取指定元素的父、子、兄弟元素。示例如下:
from pyquery import PyQuery as pq html = '''<html> <head> <title>Hello PyQuery</title> </head> <body> <ul></ul> <ul id="container"> <li class="l1">l1</li> <li class="l2">l2</li> <li class="l3">l3</li> </ul> <ul></ul> </body></html>'''doc = pq(html)ul = doc('#container')l2 = doc('#container .l2')# 获取 ul 父元素ul_parent = ul.parent()# 获取 ul 子元素ul_child = ul.children()# 获取第二个 li 兄弟元素l2_sib = l2.siblings()print(ul_parent)print(ul_child)print(l2_sib)
获取属性、文本信息
from pyquery import PyQuery as pq html = '''<html> <head> <title name='title'>Hello PyQuery</title> </head></html>'''doc = pq(html)title =doc('title')# 获取 name 属性print(title.attr('name'))# 获取 title 标签文本信息print(title.text())
获取 html
from pyquery import PyQuery as pq html = '''<html> <head> <title>Hello PyQuery</title> </head> <body> <ul id="container"> <li class="l1">l1</li> <li class="l2">l2</li> <li class="l3">l3</li> </ul> </body></html>'''doc = pq(html)# 获取 ul 中 htmlul =doc('ul')print(ul.html())
伪类可以根据一个元素的特征进行分类,下面通过示例了解下伪类选择器的使用。
from pyquery import PyQuery as pq html = '''<html> <head> <title>Hello PyQuery</title> </head> <body> <ul id="container"> <li class="l1">l1</li> <li class="l2">l2</li> <li class="l3">l3last</li> </ul> </body></html>'''doc = pq(html)# 设置起始位置lis = doc('li:gt(-1)')# 获取第一个 lifli = doc('li:first-child')# 获取最后一个 lilli = doc('li:last-child')# 获取指定 lil2 = doc('li:nth-child(2)')# 获取包含 last 的 licli = doc('li:contains("last")')print(lis)print(fli)print(lli)print(l2)print(cli)