爬虫----beautifulsoup的简单使用
beautifulSoup使用:
简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据。
pip3 install beautifulsoup4
解析器
Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果我们不安装它,则 Python 会使用 Python默认的解析器,lxml 解析器更加强大,速度更快,推荐安装。
pip3 install lxml
另一个可供选择的解析器是纯Python实现的 html5lib , html5lib的解析方式与浏览器相同,可以选择下列方法来安装html5lib:
pip install html5lib

使用:
html文档
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<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>
"""
使用
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser') #html_doc 可以使用本地的html文档,可以用网络来获取 html文档 ,此时 html_doc 是字符串
print(soup.prettify())
具体
1、soup.title
# <title>The Dormouse's story</title> 2、soup.title.name
# u'title' 2、soup.head.title = soup.find("head").find("title")
# <title>The Dormouse's story</title>
3、soup.title.string
# u'The Dormouse's story'
4、find_parent()/find_parents()
#a_string = soup.find(string="Lacie")
#print(a_string.find_parent())
#print(a_string.find_parent("p"))
#print(a_string.find_parents())
4、soup.title.parent.name
# u'head'
5、soup.p #通过点取属性的方式只能获得当前名字的第一个tag:
# <p class="title"><b>The Dormouse's story</b></p> 6、soup.p['class']
# u'title'
7、soup.a
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a> 7、soup.find_all('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>] 7、soup.find_all("a",limit=2) #限制只能找两个 7、soup.find_all("a",recursive=False) #find_all() 会检索所有的子孙节点 ,recursive=False,表示只检索 子节点
8、soup.find_all(id="id1") 9、soup.find_all(["a","p"]) #找到所有的a标签和p标签 10、soup.find_all(True) #True 可以匹配任何值,下面代码查找到所有的tag,但是不会返回字符串节点
11、soup.find_all(id=True) #找到所有的 含有 id 的标签
12、soup.find_all(href=re.compile("elsie"), id='link1') #多条件过滤
# [<a class="sister" href="http://example.com/elsie" id="link1">three</a>]
13、soup.find_all("a", class_="sister") #使用class过滤 ,不能直接使用class;class是python的关键字
#[<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>
#]
14、data_soup.find_all(attrs={"data-foo": "value"}) #通过属性查找
#[<div data-foo="value">foo!</div>]
14、soup.find(id="link3")
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
import re #使用正则
15、soup.find(string=re.compile("sisters"))
# u'Once upon a time there were three little sisters; and their names were\n' 15、soup.find_all(text=re.compile("Dormouse") #使用正则 text 参数可以搜搜文档中的字符串内容
# ["The Dormouse's story", "The Dormouse's story"]
import re #使用正则
16、for tag in soup.find_all(re.compile("^b")):
print(tag.name)
#body
#b 通过CSS选择器查找
select 方法返回的结果都是列表形式,可以遍历形式输出,然后用 get_text() 方法来获取它的内容:
1、print(soup.select("title")) #[<title>The Dormouse's story</title>]
2、print(soup.select(".sister")) #找所有的class="sister"
3、print(soup.select("#link1"))
4、print(soup.select("p #link2")) 5、print(soup.select("p > #link2"))
6、print(soup.select("a[href='http://example.com/tillie']")) #属性查找
自定义过滤器
def has_class_but_no_id(tag):
return tag.has_attr('class') and not tag.has_attr('id') print(soup.find_all(has_class_but_no_id)) '''
[
<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 class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
and they lived at the bottom of a well.
</p>,
<p class="story">...</p>
]
'''
name和attributes属性
每个tag都有自己的名字,通过 .name 来获取
tag['class'] = 'verybold'
tag['id'] = 1
tag
# <blockquote class="verybold" id="1">Extremely bold</blockquote> del tag['class']
del tag['id']
tag
# <blockquote>Extremely bold</blockquote> tag['class']
# KeyError: 'class'
print(tag.get('class'))
# None
用 .string标签内部的文字
字符串常被包含在tag内.Beautiful Soup用 NavigableString 类来包装tag中的字符串,通过 unicode() 方法可以直接将 NavigableString 对象转换成Unicode字符串:
tag.string
# u'Extremely bold'
type(tag.string)
# <class 'bs4.element.NavigableString'> unicode_string = unicode(tag.string)
unicode_string
# u'Extremely bold'
type(unicode_string)
# <type 'unicode'>
tag中包含的字符串不能编辑,但是可以被替换成其它的字符串,用 replace_with() 方法:
tag.string.replace_with("No longer bold")
tag
# <blockquote>No longer bold</blockquote>
从文档中获取所有文字内容:
print(soup.get_text())
beautifulSoup遍历文档树:
1.子节点/子孙节点
tag的
.contents 属性可以将tag的子节点以列表的方式输出:
.children 它返回的不是一个 list,不过我们可以通过遍历获取所有子节点。.childern返回的是一个list生成器对象.descendants属性可以对所有tag的子孙节点进行递归循环 。
2.父节点
.parent 获取某个元素的父节点
.parents 递归得到元素的所有父辈节点 for parent in link.parents:
if parent is None:
print(parent)
else:
print(parent.name)
3.兄弟节点
.next_sibling 获取了该节点的下一个兄弟节点
.previous_sibling 则与之相反
如果节点不存在,则返回 None
注意:实际文档中的tag的 .next_sibling 和 .previous_sibling 属性通常是字符串或空白,因为空白或者换行也可以被视作一个节点,所以得到的结果可能是空白或者换行
全部兄弟节点
.next_siblings .previous_siblings 属性 通过 .next_siblings 和 .previous_siblings 属性可以对当前节点的兄弟节点迭代输出 for sibling in soup.a.next_siblings:
print(repr(sibling))
4.前后节点
.next_element .previous_element #只找一个
注意:与 .next_sibling .previous_sibling 不同,它并不是针对于兄弟节点,而是在所有节点,不分层次
例子
<html><head><title>The Dormouse's story</title><a>ddddd</a></head> print(soup.head.next_element) #<title>The Dormouse's story</title>
print(soup.head.next_element.next_element) #The Dormouse's story
print(soup.head.next_element.next_element.next_element)
print(soup.head.next_element.next_element.next_element.next_element.next_element.next_element.next_element+“........”) #会一直往后找,递归着找,但是每一次只能找一个
所有前后节点
.next_elements .previous_elements 递归搜索所有的
通过 .next_elements 和 .previous_elements 的迭代器就可以向前或向后访问文档的解析内容,就好像文档正在被解析一样
5、节点内容 .string
如果一个标签里面没有标签了,那么 .string 就会返回标签里面的内容。如果标签里面只有唯一的一个标签了,那么 .string 也会返回最里面的内容。
如果tag包含了多个子节点,tag就无法确定,string 方法应该调用哪个子节点的内容, .string 的输出结果是 None
2.1多个内容 .strings
获取多个内容,不过需要遍历获取,比如下面的例子:
for string in soup.strings:
print(repr(string)) #会打印 “/n” 换行符
2.2多个内容 .stripped_strings 输出的字符串中可能包含了很多空格或空行,使用 .stripped_strings 可以去除多余空白内容
for string in soup.stripped_strings:
print(repr(string)) #不会打印换行符
补充
from bs4 import BeautifulSoup
soup = BeautifulSoup(open("index.html"))
soup = BeautifulSoup("<html>data</html>")
然后,Beautiful Soup选择最合适的解析器来解析这段文档,如果手动指定解析器那么Beautiful Soup会选择指定的解析器来解析文档。
爬虫----beautifulsoup的简单使用的更多相关文章
- 爬虫基础库之beautifulsoup的简单使用
beautifulsoup的简单使用 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: ''' Beautiful Soup提供一些简单的.p ...
- php爬虫最最最最简单教程
php爬虫最最最最简单教程 一.总结 一句话总结:用的爬虫框架,却是用的自己的例子(因为网站结构的变化,作者的例子不一定好用) 爬虫框架 自己例子 1.发现自己的运行效果和作者的不一样怎么办? 耐下性 ...
- 爬虫beautifulsoup实践
爬虫beautifulsoup实践: 目的:在https://unsplash.com/上爬取图片并保存到本地文件夹里. 一.观察response.首先,在Chrome浏览器里观察一下该网页的re ...
- Python爬虫之路——简单网页抓图升级版(添加多线程支持)
转载自我的博客:http://www.mylonly.com/archives/1418.html 经过两个晚上的奋斗.将上一篇文章介绍的爬虫略微改进了下(Python爬虫之路--简单网页抓图),主要 ...
- python3 调用 beautifulSoup 进行简单的网页处理
python3 调用 beautifulSoup 进行简单的网页处理 from bs4 import BeautifulSoup file = open('index.html','r',encodi ...
- #爬虫必备,解析html文档----beautifulsoup的简单用法
#出处:http://mp.weixin.qq.com/s?__biz=MjM5NzU0MzU0Nw==&mid=201820961&idx=2&sn=b729466f334d ...
- 爬虫——BeautifulSoup和Xpath
爬虫我们大概可以分为三部分:爬取——>解析——>存储 一 Beautiful Soup: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功 ...
- python BeautifulSoup的简单使用
官网:https://www.crummy.com/software/BeautifulSoup/bs4/doc/ 参考:https://www.cnblogs.com/yupeng/p/336203 ...
- python自动化之爬虫原理及简单案例
[爬虫案例]动态地图里的数据如何抓取:以全国PPP综合信息平台网站为例 http://mp.weixin.qq.com/s/BXWTf5hmq8vp91ZvgaphEw [爬虫案例]动态页面的抓取! ...
随机推荐
- UOJ #36「清华集训2014」玛里苟斯
这怎么想得到啊......... UOJ #36 题意:求随机一个集合的子集的异或和的$k$次方的期望值,保证答案$ \lt 2^{63},1 \leq k \leq 5$ $ Solution:$ ...
- Django实战(一)-----用户登录与注册系统2(数据模型、admin后台、路由视图)
使用Django开发Web应用的过程中,很多人都是急急忙忙地写视图,写前端页面,把最根本的模型设计给忽略了. 模型中定义了数据如何在数据库内保存,再直白点说就是数据表的定义.这部分工作体现在Djang ...
- zookeeper的ACL权限控制
ACL:Access Control List 访问控制列表 1. 简介 0.概述 ACL 权限控制,使用:scheme:id:perm 来标识,主要涵盖 3 个方面: 权限模式(Scheme): ...
- C++ std::pair的用法
1 pair的应用 pair是将2个数据组合成一个数据,当需要这样的需求时就可以使用pair,如stl中的map就是将key和value放在一起来保存.另一个应用是,当一个函数需要返回2个数据的时候, ...
- bootstrap模态框显示时被遮罩层遮住了
<style>.modal-backdrop{z-index:0;}</style>
- 3. SpringBoot ——自动配置原理浅析
SpringBoot的功能之所以强大,离不开它的自动配置这一大特色.但估计很多人只是知其然而不知其所以然.下面本人对自动配置原理做一个分析: 在使用SpringBoot时我们通过引入不同的Starte ...
- Suspend to RAM和Suspend to Idle分析,以及在HiKey上性能对比【转】
转自:https://www.cnblogs.com/arnoldlu/p/6253665.html 测试环境:AOSP 7.1.1+Kernel 4.4.17 HW:HiKey Ubuntu 14. ...
- WYSIWYG WebBuilder 所见即所得工具
WYSIWYG WebBuilder 所见即所得工具: http://www.wysiwygwebbuilder.com/
- mutt+msmtp实现在shell环境中发送电子邮件
作者:邓聪聪 为了自动化接收服务端的文件备份信息,利用mutt+msmtp在shell环境中发送电子邮件,轻松高效的完成运维工作. 下载msmtp wget http://downloads.sour ...
- 【转】gcc中的-w -W和-Wall选项
-w的意思是关闭编译时的警告,也就是编译后不显示任何warning,因为有时在编译之后编译器会显示一些例如数据转换之类的警告,这些警告是我们平时可以忽略的. -Wall选项意思是编译后显示所有警告. ...