Beautiful Soup
来源:互联网 发布:单页商城源码 编辑:程序博客网 时间:2024/05/22 15:28
1. Beautiful Soup的简介
Beautiful Soup 是用Python写的一个HTML/XML的解析器,它可以很好的处理不规范标记并生成剖析树(parse tree)。 它提供简单又常用的导航(navigating),搜索以及修改剖析树的操作。它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出一个完整的应用程序。Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。你不需要考虑编码方式,除非文档没有指定一个编码方式,这时,Beautiful Soup就不能自动识别编码方式了。然后,你仅仅需要说明一下原始编码方式就可以了。Beautiful Soup已成为和lxml、html6lib一样出色的python解释器,为用户灵活地提供不同的解析策略或强劲的速度。
官方文档地址:http://www.crummy.com/software/BeautifulSoup/bs4/doc/
2.Beautiful Soup的基本使用
将一段文档传入BeautifulSoup 的构造方法,就能得到一个文档的对象, 可以传入一段字符串或一个文件句柄.
from bs4 import BeautifulSoupsoup = BeautifulSoup(open("index.html"))soup = BeautifulSoup("<html>data</html>")`
首先,文档被转换成Unicode,并且HTML的实例都被转换成Unicode编码
然后,Beautiful Soup选择最合适的解析器来解析这段文档,如果手动指定解析器那么Beautiful Soup会选择指定的解析器来解析文档.
遍历文档树的常用方法
from bs4 import BeautifulSouphtml_doc = """<html><head><title>The Dormouse's story</title></head><p class="title"><b>The Dormouse's story</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>"""soup = BeautifulSoup(html_doc) #创建BeautifulSoup对象soup.head #获取head标签(该方法只能获取第一个标签)# <head><title>The Dormouse's story</title></head>soup.find_all('a') #获取所有的a标签# [<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>]soup.head.contents #返回head子节点的列表[<title>The Dormouse's story</title>]for child in title_tag.children: #通过tag的 .children 生成器,可以对tag的子节点进行循环 print(child) # The Dormouse's story
搜索文档树的常用方法
find_all( name , attrs , recursive , text , **kwargs )
find_all() 方法搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件.
name 参数可以查找所有名字为 name 的tag,字符串对象会被自动忽略掉.
keyword 参数
如果一个指定名字的参数不是搜索内置的参数名,搜索时会把该参数当作指定名字tag的属性来搜索,如果包含一个名字为 id 的参数,Beautiful Soup会搜索每个tag的”id”属性.
soup.find_all(id='link2')# [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]如果传入 href 参数,Beautiful Soup会搜索每个tag的”href”属性:soup.find_all(href=re.compile("elsie"))# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
搜索指定名字的属性时可以使用的参数值包括 字符串 , 正则表达式 , 列表, True
通过 text 参数可以搜搜文档中的字符串内容.与 name 参数的可选值一样, text 参数接受 字符串 , 正则表达式 , 列表, True .
soup.find_all(text="Elsie")# [u'Elsie']
find_all() 方法返回全部的搜索结构,如果文档树很大那么搜索会很慢.如果我们不需要全部结果,可以使用 limit 参数限制返回结果的数量.效果与SQL中的limit关键字类似,当搜索到的结果数量达到 limit 的限制时,就停止搜索返回结果.
文档树中有3个tag符合搜索条件,但结果只返回了2个,因为我们限制了返回数量:
soup.find_all("a", limit=2)# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
recursive 参数
调用tag的 find_all() 方法时,Beautiful Soup会检索当前tag的所有子孙节点,如果只想搜索tag的直接子节点,可以使用参数 recursive=False
find( name , attrs , recursive , text , **kwargs )
find_all() 方法将返回文档中符合条件的所有tag,尽管有时候我们只想得到一个结果.比如文档中只有一个标签,那么使用 find_all() 方法来查找标签就不太合适
唯一的区别是 find_all() 方法的返回结果是值包含一个元素的列表,而 find() 方法直接返回结果.
find_all() 方法没有找到目标是返回空列表, find() 方法找不到目标时,返回 None .
CSS选择器
Beautiful Soup支持大部分的CSS选择器 [6] ,在 Tag 或 BeautifulSoup 对象的 .select() 方法中传入字符串参数,即可使用CSS选择器的语法找到tag
get_text()
如果只想得到tag中包含的文本内容,那么可以嗲用 get_text() 方法,这个方法获取到tag中包含的所有文版内容包括子孙tag中的内容,并将结果作为Unicode字符串返回:
- Beautiful Soup
- Beautiful Soup
- Beautiful Soup
- Beautiful Soup
- Beautiful Soup
- Beautiful Soup 中文文档
- Beautiful Soup 中文教程
- Beautiful Soup 中文文档
- Beautiful Soup 中文文档
- python Beautiful Soup文档
- Beautiful Soup 安装
- beautiful soup功能
- Beautiful Soup安装
- HDU 4782 Beautiful Soup
- 安装beautiful soup
- Python Beautiful Soup简介
- Beautiful Soup 中文文档
- Beautiful Soup 提取网页
- 各种数据源的ConnectString【转】
- Solution for link error:Cannot Open File 'python27_d.lib'
- 设计模式 策略模式 以角色游戏为背景
- linux常用命令sed
- 设计模式——简单工厂模式分析
- Beautiful Soup
- UIViewController 相关生命周期总结 和 UIView 总结
- 主成分分析(PCA)
- 通俗理解卷积神经网络
- 如何从零开始搭建一套web集成测试环境
- 王垠:谈 Linux,Windows 和 Mac
- leetcode485 Max Consecutive Ones Java
- Markdown 教程示例
- 关于Java变量名前缀m、s等的解释