# 安装
pip install beautifulsoup4
# 看一下是否安装成功
pip list
# 导入beautifulsoup4模块,简称bs4
from bs4 import BeautifulSoup
# bs4库可以简单理解为是一个解析、遍历、维护、‘标签树’的功能库
#导入bs4模块
from bs4 import BeautifulSouphtml = """<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 ' class='sister'
id='link1'>Elsie, ' class='sister'
id='link2'>Lacie and ' class='sister' id='link3'>Tillie;
and they lived at the bottom of a well.</p> <p class='story'>...</p> </html>
"""
# html:需要解析的内容,即网页源代码所构成的字符串
# html解析器:html.parser
soup = BeautifulSoup(html,'html.parser')
# 输出结果,即把html输出,排过版的,结构清晰。
print(soup.prettify())# 输出title
soup.title
# 输出title的名字
soup.title.name
# 输出p标签
soup.p
# 找到所有的p标签
soup.find_all('p')
# 输出p标签的class属性
soup.p['class']
备注:更多关于Beautiful Soup介绍请看参考文章。
参考文章:
Beautiful Soup教程.
Beautiful Soup库(bs4)入门.
python bs4 库简介.
下楼买了萝卜粉丝饼,好吃。
本文发布于:2024-01-28 13:45:53,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17064207597836.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |