網站首頁 編程語言 正文
1、獲取節點的內容
獲取節點內容:
如果要獲得節點中的文本內容,可以用 string 或 get_text()
- string:只能獲得節點中的文本內容,如果節點中有子孫節點,string就獲取不到內容,返回 None
- get_text() 推薦使用:獲取到節點中包含的所有內容包括子孫節點中的內容
- 可以使用get_text() 方法快速得到源文件中的所有文本內容,如 soup.get_text()
使用實例:
待解析的html文本文件如下:id為al的p標簽有子孫節點,id為bl的span標簽沒有子孫節點
<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
</head>
<body>
<p class="title"/>
<a href="http://localhost:8080" />
<div>
<p class="story" id="al">
<a class="s1" href="www.baidu.com" id="l1">a1</a>
<a class="s2" href="" id=" l2">a2</a>
<a class="s3" href="" id=" l3">a3</a>
<span>span</span>
</p>
<span id="bl">
方唐鏡
</span>
</div>
</body>
</html>
使用實例1:對p標簽和span標簽進行解析,打印里面的內容
from bs4 import BeautifulSoup
#使用 lxml 解析器
soup = BeautifulSoup(open('test.html',encoding='utf-8'),'lxml')
list = soup.select('#al')[0]
print(list.string)
print(list.get_text())
執行結果及說明:
用string獲取p標簽的文本內容,因為p標簽有子孫節點,所以返回None;
get_text()獲取p標簽的文本內容,返回p標簽及子孫節點中的文本內容;
None
# 用
a1
a2
a3
使用實例2:對span標簽進行解析,打印里面的內容
span = soup.select('#bl')[0]
print(span.string)
print(span.get_text())
執行結果及說明:
string獲取span標簽的文本內容,因為span標簽沒有子孫節點,所以可以返回文本內容;
用get_text()獲取span標簽的文本內容,因為span標簽沒有子孫節點,所以只返回span標簽的文本內容;
span
?
? ? ? ? 方唐鏡
? ??? ? ? ? 方唐鏡
2、獲取節點的名稱
.name 獲取節點的名稱
待解析的html文本文件:
<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
</head>
<body>
<p class="title"/>
<a href="http://localhost:8080" />
<div>
<span id="span" class="c1">
方唐鏡
</span>
</div>
</body>
</html>
獲取節點的屬性實例:
from bs4 import BeautifulSoup
#使用 lxml 解析器
soup = BeautifulSoup(open('test.html',encoding='utf-8'),'lxml')
# 根據class選擇器查找,返回第一個節點
obj = soup.select('.c1')[0]
print(obj.name)
執行結果:該節點為span節點
span
3、獲取節點的屬性值
.attrs 獲取獲取節點的屬性值,并以字典的形式返回
待解析的html文本文件:
<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
</head>
<body>
<p class="title"/>
<a href="http://localhost:8080" />
<div>
<span id="span" class="c1">
方唐鏡
</span>
</div>
</body>
</html>
獲取節點的屬性實例:
from bs4 import BeautifulSoup
#使用 lxml 解析器
soup = BeautifulSoup(open('test.html',encoding='utf-8'),'lxml')
# 根據class選擇器查找,返回第一個節點
obj = soup.select('.c1')[0]
print(obj.attrs)
執行結果:以字典的形式返回
{'id': 'span', 'class': ['c1']}
可以通過get方法獲得字典里指定屬性的屬性值:有下面三種方法
# 獲取class屬性的屬性值
#方式一(推薦)
print(obj.attrs.get('class'))
#方式二
print(obj.get('class'))
#方式三
print(obj['class'])
執行結果:
['c1']
['c1']
['c1']
3、BS4具體使用
代碼實例:獲取所有飲品的名稱·
import urllib.request
from bs4 import BeautifulSoup
from lxml import etree
url = 'https://www.starbucks.com.cn/menu/'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.0.2242 SLBChan/10'
}
# 定制請求,發送請求并返回響應對象和html文檔
request = urllib.request.Request(url=url,headers=headers)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
# 使用 lxml 解析器
soup = BeautifulSoup(content,'lxml')
# 檢索html文檔,返回列表形式
name_list = soup.select('ul[class="grid padded-3 product"] strong')
# 遍歷打印
for name in name_list:
print(name.string)
執行結果:
原文鏈接:https://juejin.cn/post/7135056230812221477
相關推薦
- 2022-02-02 appname is automatically signed for development,
- 2022-07-20 Python實現向PPT中插入表格與圖片的方法詳解_python
- 2022-10-13 詳解python-opencv?常用函數_python
- 2023-08-01 el-table 拖拽列寬出現空白列問題
- 2023-01-12 C語言中數組排序淺析_C 語言
- 2022-05-27 一起來學習C語言的程序環境與預處理_C 語言
- 2022-06-01 Go中的gRPC入門教程詳解_Golang
- 2022-10-17 C++模擬實現vector的示例代碼_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同步修改后的遠程分支