爬虫----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 [爬虫案例]动态页面的抓取! ...
随机推荐
- Python Tornado简介
简介 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了 ...
- 0326 iframe和video experience
今天的东西确实不少,很容易混淆,input下的属性太多,form下的属性也不少,内容多点,一时不能熟练掌握,晚上多拿出点时间练练 尤其是 form 和inpu那些属性格外别扭 下午的内嵌视频相对简单主 ...
- Javascript - ExtJs - TreePanel组件
TreePanel组件(Ext.tree.TreePanel)logogram:Ext.tree.Panel | xtype:treepanel 树与节点 树面板组件的根是源头,从根上拓展出其它的子节 ...
- cmder使用简介
简介 cmder是一个增强型命令行工具,不仅可以使用windows下的所有命令,更爽的是可以使用linux的命令,shell命令. 下载 官网地址:http://cmder.net/ 下载的时候,会有 ...
- MySql cmd下的学习笔记 —— 有关视图的操作(algorithm)
建立一张简单的查询视图,不用临时表,只用条件合并 在简单的查询中,建立临时表的开销比较大 这是可以指定algorithm选项为merge 在v2的视图中,并没有建立临时表 但在下列情况下,必须建立临时 ...
- spring源码学习2
spring总览 从入口看起 我们用spring时会用ClassPathXmlApplicationContext来加载spring配置文件,就从它开始吧. 1.双击shhift,输入ClassPat ...
- sqlserver开窗函数在财务对账中的用法
曾几何时发现开窗函数在财务对账总特别好用.但是每次可能很久没用,逻辑都要重头来过.特此留一份完整的思考逻辑待日后参考. 以下是数据源: 从上面的数据可以看到通过C列,那么只需要两个条件即可获得已经用对 ...
- linux的/etc/profile环境变量设置不生效【原创】
设置/etc/profile的java环境变量不生效 修改环境变量 /etc/profile JAVA_HOME=/opt/software/jdk1..0_25 PATH=/usr/local/sb ...
- nginx 配置域名转发
自己测试环境,配置下载目录和一个jenkins的地址: 域名跳转,反向代理 # cat ../nginx.conf user www www; worker_processes ; error_log ...
- $.each() 与 $(selector).each()的区别
$.each( dataArr,function(i,item){}) 可用于遍历任何的集合(无论是数组或对象). 如果是数组,回调函数每次传入数组的索引(也就是i)和对应的值(item)(值亦可以 ...