BeautifulSoup的高级应用 之 find findAll
来源:互联网 发布:powerdvd源码输出 编辑:程序博客网 时间:2024/05/18 22:18
BeautifulSoup 是Python学习的重要组成部分,可用于帮助解析html/XML等内容,尤其是在爬取特定网页信息的时候,用于解析和检查在网上看到的那些乱七八糟而且不规范的HTML页面。至于BeautifulSoup 模块的安装可以参考博客
至于如何获取网页内容,可以查看博客内容总结。
这些方法的单数形式对应着某个复数形式,会找到所有符合要求的tag,以list的方式放回。他们的对应关系是:find->findall, findParent->findParents, findNextSibling->findNextSiblings…
举一个简单的爬取百度网页的例子:
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
这里的BeautifulSoup(content)函数 在官网上的解释为:Beautiful Soup is a Python library for pulling data out of HTML and XML files. It works with your favorite parser to provide idiomatic ways of navigating, searching, and modifying the parse tree. It commonly saves programmers hours or days of work.
将复杂HTML文档转换成一个复杂的树形结构.每个节点都是Python对象.
BeautifulSoup的主要函数使用
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
这里是一个读取html标签 然后通过prettify()函数输出标签的过程。这里输出soup对象的html标签有多种方法:
1 soup.prettify()
2 soup.html
3 soup.contents
4 soup
另外使用soup+标签名称可以获取html标签中第一个匹配的标签内容,举例:
print soup.p输出结果为:<p class="title"><b>The Dormouse's story</b></p>
print soup.p.string 输出标签的内容 结果为:The Dormouse's story
另外输出标签内容还可以使用get_text()函数:
- 1
- 2
- 3
- 1
- 2
- 3
通过get函数获得标签的属性:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
对其他的标签也是同样可用的,并且输出的结果为文档中第一个匹配的对象,如果要搜索其他的标签需要使用find findAll函数。
BeautifulSoup提供了强大的搜索函数find 和findall,这里的两个方法(findAll和 find)仅对Tag对象以及,顶层剖析对象有效。
findAll(name, attrs, recursive, text, limit, **kwargs)
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
findAll也可以使用标签的属性搜索标签,寻找 id=”secondpara”的 p 标记,返回一个结果集:
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
利用正则表达式搜索tag标签内容:
- 1
- 2
- 3
- 1
- 2
- 3
利用标签的多个属性值进行搜索:
- 1
- 2
- 3
- 1
- 2
- 3
对搜索结果的个数进行限制: limit=n
- 1
- 2
- 3
- 1
- 2
- 3
利用find_all搜索返回一个列表:
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
这里的find_all函数参数中设置了一个列表的形式,包含了a和b两个标签,使结果以列表的形式返回。
读取和修改属性:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
BeautifulSoup中的find和findAll用法相同,不同之处为find返回的是findAll搜索值的第一个值。举例:
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
推荐:
BeautifulSoup的安装可以查看博客。
python中的模块安装及更新方法,可以查看博客(pip,easy_install)。
解决python中出现乱码问题,可以查看博客。
- BeautifulSoup的高级应用 之 find findAll
- BeautifulSoup的高级应用 之 find findAll
- Python的BeautifulSoup的find()和findAll()
- Python爬虫:BeautifulSoup的find()和findAll()
- BeautifulSoup库之find、findAll和children、descendants
- python - BeautifulSoup中的find()和findAll()
- 使用BeautifulSoup中的find()和findAll()函数时关键字参数的注意事项
- ③BeautifulSoup中的find()函数和findAll()函数
- Python的BeautifulSoup之find和find_all
- BeautifulSoup的高级应用 之 contents children descendants string strings stripped_strings
- BeautifulSoup的高级应用 之.parent .parents .next_sibling.previous_sibling.next_siblings.previous_siblings
- BeautifulSoup高级应用 之 CSS selectors /CSS 选择器
- BeautifulSoup高级应用 之 CSS selectors /CSS 选择器
- Python ElementTree的find,findall函数参数
- C# Find() 与 FindAll()方法的使用
- List(T) 的实现实体对象操作,之Find FindAll Contains ForEach Sort
- BeautifulSoup的简单应用
- List的Sort()、Find()、FindAll()、Exist()的使用方法举例
- 10-NSPersistentContainer介绍
- 11-使用NSPersistentContainer搭建CoreData Stack
- 12-CoreData操作大量数据时的注意细节
- 13-NSPersistentContainer性能比较
- 14-CoreData兼容iOS9和iOS10
- BeautifulSoup的高级应用 之 find findAll
- 搬家了,以后 在 www.JiFei.co 上写文章了。。。
- LeetCode 465. Optimal Account Balancing
- Leetcode 131. Palindrome Partitioning (Medium) (cpp)
- Volley 解析
- STL之map实现(一)
- STL之map实现(二)
- Sqoop 各大命令解释
- 前段时间做项目中遇到问题的随笔分享