1.安装
pip install beautifulsoup4
或者
python setup.py install(附件)
2.介绍
解析(markup为html文件内容)
BeautifulSoup(markup, “html.parser”)-》Python 2.7.3以上
BeautifulSoup(markup, “lxml”)-》速度快
BeautifulSoup(markup, “html5lib”)-》容错性好,速度慢from bs4
3.举例
import BeautifulSoup
soup = BeautifulSoup(html)
#soup = BeautifulSoup(open('index.html'))
print soup.prettify()#格式化
4.使用
1.TAG
html1='<head><title>The Dormouse's story</title></head>'
soup = BeautifulSoup(html1)
print soup.title
#<title>The Dormouse's story</title>#仅仅取第一条
print soup.head
#<head><title>The Dormouse's story</title></head>
2.attrs
html2='<p class="title" name="dromouse"><b>The Dormouse's story</b></p>'
soup = BeautifulSoup(html2)
print soup.p.attrs
#{'class': ['title'], 'name': 'dromouse'}
print soup.p['class']
#['title']
print soup.p.get('class')
#['title']
3.string
print soup.p.string
#The Dormouse's story
4.Comment
无注释的文本
html3='<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>'
soup = BeautifulSoup(html2)
if type(soup.a.string)==bs4.element.Comment:
print soup.a.string
5.contents
print soup.head.contents #打印子节点,为list集合
#[<title>The Dormouse's story</title>]
6.children
for child in soup.body.children:#list生成器子节点
print child
7.descendants
for child in soup.descendants:#递归打印所有子节点
print child
8.stripped_strings
for string in soup.stripped_strings:#子节点下所有string,且去除换行符等空白
print(repr(string))
# u"The Dormouse's story"
# u"The Dormouse's story"
# u'Once upon a time there were three little sisters
9.parent parents
p = soup.p#父节点
print p.parent.name
#body</strong>
10.find_all( name , attrs , recursive , text , **kwargs )
1)name-tag内容
A.传字符串
soup.find_all('b')
# [<b>The Dormouse's story</b>]
B.传正则表达式
import re
for tag in soup.find_all(re.compile("^b")):
print(tag.name)
# body
# b
C.传列表
soup.find_all(["a","b"])
# [<b>The Dormouse's story</b>,
#<a href="http://example.com/elsie" id="link1">Elsie</a>,
#<a href="http://example.com/lacie" id="link2">Lacie</a>]
D.传 True
True 可以匹配任何值,但是不会返回字符串节点
For tag in soup.find_all(True):
print(tag.name)
# html
# head
# title
# body
# p
E.传方法
def has_class_but_no_id(tag):
return tag.has_attr('class') and not tag.has_attr('id')
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...</p>,
# <p class="story">...</p>]
2)keyword
soup.find_all(id='link2')
# [<a href="http://example.com/lacie" id="link2">Lacie</a>]
soup.find_all(href=re.compile("elsie"))
# [<a href="http://example.com/elsie" id="link1">Elsie</a>]
soup.find_all(href=re.compile("elsie"), id='link1')
# [<a href="http://example.com/elsie" id="link1">three</a>]
Class为关键字,需加下划线
soup.find_all("a", class_="sister")
有些tag属性在搜索不能使用,比如HTML5中的 data-* 属性
data_soup.find_all(attrs={"data-foo": "value"})
# [<div data-foo="value">foo!</div>]
3)text 参数
通过 text 参数可以搜搜文档中的字符串内容.与 name 参数的可选值一样, text 参数接受 字符串 , 正则表达式 , 列表, True
soup.find_all(text="Elsie")
# [u'Elsie']
soup.find_all(text=["Tillie", "Elsie", "Lacie"])
# [u'Elsie', u'Lacie', u'Tillie']
soup.find_all(text=re.compile("Dormouse"))
[u"The Dormouse's story", u"The Dormouse's story"]
4)limit 参数
当搜索到的结果数量达到 limit 的限制时,就停止搜索返回结果.
soup.find_all("a", limit=2)
# [<a href="http://example.com/elsie" id="link1">Elsie</a>,
# <a href="http://example.com/lacie" id="link2">Lacie</a>]
5)recursive 参数
调用tag的 find_all() 方法时,Beautiful Soup会检索当前tag的所有子孙节点,如果只想搜索tag的直接子节点,可以使用参数 recursive=False .
11.find( name , attrs , recursive , text , **kwargs )
它与 find_all() 方法唯一的区别是 find_all() 方法的返回结果是值包含一个元素的列表,而 find() 方法直接返回结果
12.find_parents() find_parent()
13.CSS选择器select
soup.select(),返回类型是 list
(1)通过标签名查找
print soup.select('title')
#[<title>The Dormouse's story</title>]
print soup.select('a')
(2)通过类名查找
print soup.select('.sister')
#[<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>]
(3)通过 id 名查找
print soup.select('#link1')
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
(4)组合查找
组合查找即和写 class 文件时,标签名与类名、id名进行的组合原理是一样的,例如查找 p 标签中,id 等于 link1的内容,二者需要用空格分开
print soup.select('p #link1')
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
(5)直接子标签查找
print soup.select("head > title")
#[<title>The Dormouse's story</title>]
(6)属性查找
print soup.select('a[class="sister"]')
#[<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)通配符
print soup.select('a[class*="sister"]')#包含sister
print soup.select('a[class$="sister"]')# sister结尾
print soup.select('a[class^="sister"]') # sister开头
相关推荐
Beautiful Soup 的使用。 >>> from bs4 import BeautifulSoup >>> soup = BeautifulSoup(' data ',"html.parser") '''parser为解析器。具体啥意思不知道。。。''' 实际操作: >>> import requests >>> r = requests....
beautiful-soup-4.pdf python bs4 tutorial small and practical
python爬虫-Beautiful Soup库入门说明Beautiful Soup库安装Beautiful Soup库的引用Beautiful Soup库解析器Beautiful Soup类的基本元素例子tag标签tag的name(名字)tag的Attributes(属性)Tag的NavigableString...
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库....你可能在寻找 Beautiful Soup3 的文档,Beautiful Soup 3 目前已经停止开发,我们推荐在现在的项目中使用Beautiful Soup 4, 移植到BS4
Python 爬虫 虎牙主播热度排名、礼物榜 beautiful soup bs4 浏览器多页爬虫 jupyter notebook
1.Beautiful Soup提供了一些简单的方法和Python术语,用于检索和修改语法树:一个用于解析文档并提取相关信息的工具包。这样你写一个应用不需要写很多代码。 2.Beautiful Soup自动将输入文档转换为Unicode编码,并将...
[Beautiful Soup](https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/) 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup...
此外PyPi中还有一个名字是 BeautifulSoup 的包,那是 Beautiful Soup3 的发布版本.在这里不建议安装. pip install beautifulsoup4 Debain或ubuntu安装方式 apt-get install Python-bs4 你也可以通过源码安装,下载...
Python的requests和Beautiful Soup(bs4)库结合实现的亚马逊价格追踪器工具是一个强大的应用程序,旨在帮助用户监控亚马逊商品价格的变化。该工具利用requests库发送网络请求获取亚马逊网页的HTML内容,然后使用...
③验证是否可以运行成功,运行cmd执行,引用模块import bs4回车未报错,则证明安装完成,可以正常使用了: 安装方法二(像我们公司这种各种网络限制,使用pip就会出现无法安装,一直循环在retry): ①进入官网下载压缩包:...
Beautiful Soup 是用Python 写的一个HTML/XML 的解析器,它可以很好的处理 不规范标记并生成剖析树(parse tree)。它提供简单又常用的导航 (navigating),搜索以及修改剖析树的操作。它可以大大节省你的编程时 间
api-哇-python 获取 WoW API 结果的脚本 ( ) ...美汤 http://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-beautiful-soup 用法 λ ~/git/api-wow-docs/wow_api/ master* ./wow_api.py __ __ __
目录1. 简介2....最新版本Beautiful Soup 4 简称bs4。优势:相比于ET库, 功能更全,可以选择解析器来解析文档,既支持html, 也支持xml,容错度(简单理解为文档格式自动补全功能)也更高,API也很好用。 2
完整介绍了python爬虫必备库BeautifulSoup4库里的几乎所有内容: Table Of Contents - Beautiful Soup 4.2.0 文档 - 对象的种类 - Tag 标签 - Name Tag的名字 - Attributes 操作类似于字典 比如id,class_ - ...
Python 58同城房价bs4 beautiful soup爬虫获取 room_name room_type room_area room_addr0 room_addr1 room_price房价名称类型面积地址价格等 jieba中文分词 tf-idf向量化 kmeans聚类 浏览器多页爬虫 jupyter ...
Beautiful Soup库也叫beautifulsoup4,还叫bs4 一、beautiful soup库的安装 win+R,输入cmd打开命令行 输入pip install beautifulsoup4 BeautifulSoup小测:以https://www.python123.io/ws/demo.html页面的代码为例...
Python爬虫是一种自动化程序,用于从互联网上获取信息并进行处理。爬虫通常用于抓取网页内容、提取数据或监控网站变化等任务。...from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'html.parser') Sc
中文版库 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转 换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数...项目中使用Beautiful Soup 4, 移植到BS4
Beautiful Soup 库一般被称为bs4库,支持Python3,是我们写爬虫非常好的第三方库。因用起来十分的简便流畅。所以也被人叫做“美味汤”。目前bs4库的最新版本是4.60。下文会介绍该库的最基本的使用,具体详细的细节...