網站首頁 編程語言 正文
一、模塊簡介
Beautiful Soup 是一個可以從HTML或XML文件中提取數據的Python庫.它能夠通過你喜歡的轉換器實現慣用的文檔導航,查找,修改文檔的方式.Beautiful Soup會幫你節省數小時甚至數天的工作時間.
二、方法利用
1、引入模塊
# 引入
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 class="sister" id="link1">Elsie</a>,
<a class="sister" id="link2">Lacie</a> and
<a 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')
四種解析器
2、幾個簡單的瀏覽結構化數據的方法
#獲取Tag,通俗點就是HTML中的一個個標簽
#獲取Tag,通俗點就是HTML中的一個個標簽
soup.title # 獲取整個title標簽字段:<title>The Dormouse's story</title>
soup.title.name # 獲取title標簽名稱 :title
soup.title.parent.name # 獲取 title 的父級標簽名稱:head
soup.p # 獲取第一個p標簽字段:<p class="title"><b>The Dormouse's story</b></p>
soup.p['class'] # 獲取第一個p中class屬性值:title
soup.p.get('class') # 等價于上面
soup.a # 獲取第一個a標簽字段
soup.find_all('a') # 獲取所有a標簽字段
soup.find(id="link3") # 獲取屬性id值為link3的字段
soup.a['class'] = "newClass" # 可以對這些屬性和內容等等進行修改
del bs.a['class'] # 還可以對這個屬性進行刪除
soup.find('a').get('id') # 獲取class值為story的a標簽中id屬性的值
soup.title.string # 獲取title標簽的值 :The Dormouse's story
三、具體利用
1、獲取擁有指定屬性的標簽
方法一:獲取單個屬性
soup.find_all('div',id="even") # 獲取所有id=even屬性的div標簽
soup.find_all('div',attrs={'id':"even"}) # 效果同上
方法二:
soup.find_all('div',id="even",class_="square") # 獲取所有id=even并且class=square屬性的div標簽
soup.find_all('div',attrs={"id":"even","class":"square"}) # 效果同上
2、獲取標簽的屬性值
方法一:通過下標方式提取
for link in soup.find_all('a'):
print(link['href']) //等同于 print(link.get('href'))
方法二:利用attrs參數提取
for link in soup.find_all('a'):
print(link.attrs['href'])
3、獲取標簽中的內容
divs = soup.find_all('div') # 獲取所有的div標簽
for div in divs: # 循環遍歷div中的每一個div
a = div.find_all('a')[0] # 查找div標簽中的第一個a標簽
print(a.string) # 輸出a標簽中的內容
如果結果沒有正確顯示,可以轉換為list列表
4、stripped_strings
去除\n換行符等其他內容 stripped_strings
divs = soup.find_all('div')
for div in divs:
infos = list(div.stripped_strings) # 去掉空格換行等
bring(infos)
四、輸出
1、格式化輸出prettify()
prettify() 方法將Beautiful Soup的文檔樹格式化后以Unicode編碼輸出,每個XML/HTML標簽都獨占一行
markup = '<a >I linked to <i>example.com</i></a>'
soup = BeautifulSoup(markup)
soup.prettify()
# '<html>\n <head>\n </head>\n <body>\n <a >\n...'
print(soup.prettify())
# <html>
# <head>
# </head>
# <body>
# <a >
# I linked to
# <i>
# example.com
# </i>
# </a>
# </body>
# </html>
2、get_text()
如果只想得到tag中包含的文本內容,那么可以調用 get_text() 方法,這個方法獲取到tag中包含的所有文版內容包括子孫tag中的內容,并將結果作為Unicode字符串返回:
markup = '<a >\nI linked to <i>example.com</i>\n</a>'
soup = BeautifulSoup(markup)
soup.get_text()
u'\nI linked to example.com\n'
soup.i.get_text()
u'example.com'
原文鏈接:https://blog.csdn.net/Thunderclap_/article/details/128948135
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-04-09 SpringBoot 項目在Linux 環境下,日志文件logback撐爆云服務器
- 2024-02-25 maven打包測試jar包沖突
- 2021-11-21 ASP.NET?Core應用JWT進行用戶認證及Token的刷新方案_實用技巧
- 2022-04-06 Go?字符串比較的實現示例_Golang
- 2022-01-31 RuntimeError:Given input size:(256x1x1). Calculate
- 2023-11-11 ValueError: (‘Unrecognized keyword arguments:‘, di
- 2022-09-05 C語言之sizeof與strlen的使用及區別_C 語言
- 2022-05-03 C#設計模式之工廠模式_C#教程
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支