Python BautifulSoup 節(jié)點信息
1、獲取節(jié)點的內(nèi)容
獲取節(jié)點內(nèi)容:
如果要獲得節(jié)點中的文本內(nèi)容,可以用 string 或 get_text()
- string:只能獲得節(jié)點中的文本內(nèi)容,如果節(jié)點中有子孫節(jié)點,string就獲取不到內(nèi)容,返回 None
- get_text() 推薦使用:獲取到節(jié)點中包含的所有內(nèi)容包括子孫節(jié)點中的內(nèi)容
- 可以使用get_text() 方法快速得到源文件中的所有文本內(nèi)容,如 soup.get_text()
使用實例:
待解析的html文本文件如下:id為al的p標(biāo)簽有子孫節(jié)點,id為bl的span標(biāo)簽沒有子孫節(jié)點
<!DOCTYPE html> <html lang="en"> <head> <title>test</title> </head> <body> <p class="title"/> <a href="http://localhost:8080" rel="external nofollow" rel="external nofollow" rel="external nofollow" /> <div> <p class="story" id="al"> <a class="s1" href="www.baidu.com" rel="external nofollow" id="l1">a1</a> <a class="s2" href="" id=" rel="external nofollow" rel="external nofollow" l2">a2</a> <a class="s3" href="" id=" rel="external nofollow" rel="external nofollow" l3">a3</a> <span>span</span> </p> <span id="bl"> 方唐鏡 </span> </div> </body> </html>
使用實例1:對p標(biāo)簽和span標(biāo)簽進行解析,打印里面的內(nèi)容
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())
執(zhí)行結(jié)果及說明:
用string獲取p標(biāo)簽的文本內(nèi)容,因為p標(biāo)簽有子孫節(jié)點,所以返回None;
get_text()獲取p標(biāo)簽的文本內(nèi)容,返回p標(biāo)簽及子孫節(jié)點中的文本內(nèi)容;
None # 用 a1 a2 a3
使用實例2:對span標(biāo)簽進行解析,打印里面的內(nèi)容
span = soup.select('#bl')[0] print(span.string) print(span.get_text())
執(zhí)行結(jié)果及說明:
string獲取span標(biāo)簽的文本內(nèi)容,因為span標(biāo)簽沒有子孫節(jié)點,所以可以返回文本內(nèi)容;
用get_text()獲取span標(biāo)簽的文本內(nèi)容,因為span標(biāo)簽沒有子孫節(jié)點,所以只返回span標(biāo)簽的文本內(nèi)容;
span
方唐鏡
方唐鏡
2、獲取節(jié)點的名稱
.name 獲取節(jié)點的名稱
待解析的html文本文件:
<!DOCTYPE html> <html lang="en"> <head> <title>test</title> </head> <body> <p class="title"/> <a href="http://localhost:8080" rel="external nofollow" rel="external nofollow" rel="external nofollow" /> <div> <span id="span" class="c1"> 方唐鏡 </span> </div> </body> </html>
獲取節(jié)點的屬性實例:
from bs4 import BeautifulSoup #使用 lxml 解析器 soup = BeautifulSoup(open('test.html',encoding='utf-8'),'lxml') # 根據(jù)class選擇器查找,返回第一個節(jié)點 obj = soup.select('.c1')[0] print(obj.name)
執(zhí)行結(jié)果:該節(jié)點為span節(jié)點
span
3、獲取節(jié)點的屬性值
.attrs 獲取獲取節(jié)點的屬性值,并以字典的形式返回
待解析的html文本文件:
<!DOCTYPE html> <html lang="en"> <head> <title>test</title> </head> <body> <p class="title"/> <a href="http://localhost:8080" rel="external nofollow" rel="external nofollow" rel="external nofollow" /> <div> <span id="span" class="c1"> 方唐鏡 </span> </div> </body> </html>
獲取節(jié)點的屬性實例:
from bs4 import BeautifulSoup #使用 lxml 解析器 soup = BeautifulSoup(open('test.html',encoding='utf-8'),'lxml') # 根據(jù)class選擇器查找,返回第一個節(jié)點 obj = soup.select('.c1')[0] print(obj.attrs)
執(zhí)行結(jié)果:以字典的形式返回
{'id': 'span', 'class': ['c1']}
可以通過get方法獲得字典里指定屬性的屬性值:有下面三種方法
# 獲取class屬性的屬性值 #方式一(推薦) print(obj.attrs.get('class')) #方式二 print(obj.get('class')) #方式三 print(obj['class'])
執(zhí)行結(jié)果:
['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' } # 定制請求,發(fā)送請求并返回響應(yīng)對象和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)
執(zhí)行結(jié)果:
到此這篇關(guān)于Python BautifulSoup 節(jié)點信息的文章就介紹到這了,更多相關(guān)Python BautifulSoup 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用?OpenAI?API?和?Python?使用?GPT-3的操作方法
這篇文章主要介紹了使用?OpenAI?API?和?Python?使用?GPT-3,在本文中,我們將使用?GPT-3。我將向您展示如何訪問它,并提供一些示例來說明您可以使用它做什么,以及您可以使用它構(gòu)建什么樣的應(yīng)用程序,需要的朋友可以參考下2023-03-03Python使用微信itchat接口實現(xiàn)查看自己微信的信息功能詳解
這篇文章主要介紹了Python使用微信itchat接口實現(xiàn)查看自己微信的信息功能,結(jié)合實例形式分析了Python微信itchat模塊常見功能與操作技巧,需要的朋友可以參考下2019-08-08python實現(xiàn)的jpg格式圖片修復(fù)代碼
這篇文章主要介紹了python實現(xiàn)的jpg格式圖片修復(fù)代碼,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-04-04Python?任務(wù)自動化工具nox?的配置與?API詳情
這篇文章主要介紹了Python?任務(wù)自動化工具nox?的配置與?API詳情,Nox?會話是通過被@nox.session裝飾的標(biāo)準(zhǔn)?Python?函數(shù)來配置的,具體詳情下文相關(guān)介紹需要的小伙伴可以參考一下2022-07-07python標(biāo)準(zhǔn)庫 datetime的astimezone設(shè)置時區(qū)遇到的坑及解決
這篇文章主要介紹了python標(biāo)準(zhǔn)庫 datetime的astimezone設(shè)置時區(qū)遇到的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09