爬虫----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 中的比较:is 与 ==
转载: https://www.cnblogs.com/kiko0o0/p/8135184.html 在 Python 中会用到对象之间比较,可以用 ==,也可以用 is .但是它们的区别是什么呢? ...
- Spring重温(三)--Spring依赖注入(DI)
前言:在Spring框架中,DI(依赖注入)是用来定义对象彼此间的依赖,主要有set方法注入和构造器注入两种方式.另外,当一个类包含多个构造函数带的参数相同,它总是会造成构造函数注入参数类型歧义的问题 ...
- Linux C++ TCP Socket传输文件或图片实例
环境:Linux 语言:C++ 通信方式:TCP 下面用TCP协议编写一个简单的服务器.客户端,其中服务器端一直监听本机的6666号端口.如果收到连接请求,将接收请求并接收客户端发来的消息:客户端与服 ...
- dubbo初步认识
dubbo中文网站:http://dubbo.apache.org/zh-cn/ dubbo英文网站:http://dubbo.apache.org/en-us/ 1.Apache Dubbo是一款高 ...
- .net中的设计模式---单例模式
.net设计模式: Net设计模式实例之单例模式( Singleton Pattern) 一 : 单例模式的简介:(Brief Introduction) 单例模式(Singleton Patter ...
- Pytorch 细节记录
1. PyTorch进行训练和测试时指定实例化的model模式为:train/eval eg: class VAE(nn.Module): def __init__(self): super(VAE, ...
- Cascade R-CNN论文讲解(转载)
转载链接:https://blog.csdn.net/qq_21949357/article/details/80046867 论文思想:为了解决IOU设置带来的最终的AP值,作者引入了cascade ...
- better-scroll和swiper使用中的坑
better-scroll 1.我的 better-scroll 初始化了, 但是没法滚动. 按照文档使用后,发现可以原生滚动,但是不能弹性滑动,后面发现是最外层容器没给设置固定高度 ,设置固定 ...
- boost 实现http断点续传
// testc.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...
- 题解-PKUWC2018 Slay the Spire
Problem loj2538 Solution 在考场上当然要学会写暴力,考虑如果手上已经有了\(a\)张攻击牌和\(b\)张强化牌: 首先强化牌会在攻击牌之前用(废话),其次要将两种牌分别从大往小 ...