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