BeautifulSoup示例:
#!/usr/bin/env python # -*- coding: utf-8 -*- # author: imcati html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b><b>123</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc, 'html.parser') #格式化输出对象内容 #print(soup.prettify()) #根据标签名获取整个标签,取第一个值 print(soup.a) #获取标签的名字 print(soup.title.name) #获取标签中的文本 print(soup.title.string) #获取title标签的父标签 print(soup.title.parent.name) #获取p标签的子标签 print(soup.p.contents) #获取标签的属性值(两种方式) print(soup.p["class"]) print(soup.p.attrs["class"]) #使用select、css选择器 类名前加.,id名前加# print(soup.select("a")) print(soup.select(".title")) #获取内容 print(soup.select(".title")[0]) print(soup.select(".title")[0].get_text()) #获取属性值 print(soup.select(".title")[0].attrs["class"]) #获取p下面的子标签内容 print(soup.select('p > b')[1].get_text()) #使用find、findall进行查找 find返回第一个查找结果,find_all返回所有查找结果 print(soup.find('p',attrs={"class":"title"})) print(soup.find_all('p',attrs={"class":"title"})) 输出: <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a> title The Dormouse's story head [<b>The Dormouse's story</b>, <b>123</b>] ['title'] ['title'] [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] [<p class="title"><b>The Dormouse's story</b><b>123</b></p>] <p class="title"><b>The Dormouse's story</b><b>123</b></p> The Dormouse's story123 ['title'] 123 <p class="title"><b>The Dormouse's story</b><b>123</b></p>
[<p class="title"><b>The Dormouse's story</b><b>123</b></p>]
PyQuery示例:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: imcati
from pyquery import PyQuery as pq
html = """
<html lang="en">
<head>
<title>PyQuery</title>
</head>
<body>
<ul id="container">
<li class="o1">MM</li>
<li class="o2 active">MN<a class='o22'>fad</a></li>
<li class="o3">GN</li>
</ul>
</body>
</html>
"""
#初始化为PyQuery对象
doc = pq(html)
print(type(doc))
print(doc)
输出:
<class 'pyquery.pyquery.PyQuery'>
<html lang="en">
<head>
<title>PyQuery</title>
</head>
<body>
<ul id="container">
<li class="o1">MM</li>
<li class="o2 active">MN<a class="o22">fad</a></li>
<li class="o3">GN</li>
</ul>
</body>
</html>
#将HTML文件初始化
#doc = pq(filename = './pyquery_demo.html') print(type(doc))
#print(doc)
#对网址响应进行初始化
#doc = pq(url='http://www.baidu.com')
#print(type(doc))
#print(doc)
#用css选择器来实现,如果要选id前面加#,如果选class,前面加.,如果选标签名,什么也不加
doc = pq(html)
# 根据标签
print(doc('title'))
print(doc('.o1'))
输出:
<title>PyQuery</title>
<li class="o1">MM</li>
# 组合标签
print(doc('.o2.active')) #空格表示里面,没有空格表示整体
print(doc('.o2 .o22'))#空格表示里面,没有空格表示整体
输出:
<li class="o2 active">MN<a class="o22">fad</a></li>
<a class="o22">fad</a>
# 伪类选择器
print(doc('li:nth-child(2)'))
# 根据标签内容获取标签
print(doc("li:contains('MM')"))
输出:
<li class="o2 active">MN<a class="o22">fad</a></li>
<li class="o1">MM</li>
#利用find方法
print(doc.find('li'))
# 也可以用.children()查找直接子元素
container = doc.find('#container')
print(container.children())
输出:
<li class="o1">MM</li>
<li class="o2 active">MN<a class="o22">fad</a></li>
<li class="o3">GN</li>
<li class="o1">MM</li>
<li class="o2 active">MN<a class="o22">fad</a></li>
<li class="o3">GN</li>
#.parent()查找对象的父元素
object_2 = doc.find('.o2')
print(object_2.parent())
输出:
<ul id="container">
<li class="o1">MM</li>
<li class="o2 active">MN<a class="o22">fad</a></li>
<li class="o3">GN</li>
</ul>
# #.parents()祖先节点
object_2 = doc.find('.o2')
parent = object_2.parents('#container')
#当然也可以传入参数
print(parent)
输出:
<ul id="container">
<li class="o1">MM</li>
<li class="o2 active">MN<a class="o22">fad</a></li>
<li class="o3">GN</li>
</ul>
#.siblings()兄弟元素,即同级别的元素,不包括自己
object_2 = doc.find('.o2')
print(object_2.siblings())
输出:
<li class="o1">MM</li>
<li class="o3">GN</li>
# 遍历
lis = doc('li').items() #.items会是一个生成器 # print(type(lis))
for li in lis:
print(li)
输出:
<li class="o1">MM</li>
<li class="o2 active">MN<a class="o22">fad</a></li>
<li class="o3">GN</li>
# filter() 根据类名、id名得到指定元素,例:
d=pq("<div><p id='1'>test 1</p><p id='2'>test 2</p></div>")
print(d('p').filter('#1')) #返回[<p#1>]
print(d('p').filter('#2')) #返回[<p.2>]
# eq(index) 根据给定的索引号得到指定元素
# 接上例,若想得到第二个p标签内的内容,则可以:
print (d('p').eq(1).html()) #返回test 2
输出:
<p id="1">test 1</p>
<p id="2">test 2</p>
test 2
# 获取属性值
o2 = doc.find('.o2')
print(o2.attr('class'))
# 获取文本值
o2 = doc.find('.o2')
print(o2.text())
输出:
o2 active
MNfad