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)簽沒有子孫節(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é)果及說明:
用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é)果及說明:
string獲取span標(biāo)簽的文本內(nèi)容,因?yàn)閟pan標(biāo)簽沒有子孫節(jié)點(diǎn),所以可以返回文本內(nèi)容;
用get_text()獲取span標(biāo)簽的文本內(nèi)容,因?yàn)閟pan標(biāo)簽沒有子孫節(jié)點(diǎn),所以只返回span標(biāo)簽的文本內(nèi)容;
span
方唐鏡
方唐鏡
2、獲取節(jié)點(diǎn)的名稱
.name 獲取節(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.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']}可以通過get方法獲得字典里指定屬性的屬性值:有下面三種方法
# 獲取class屬性的屬性值
#方式一(推薦)
print(obj.attrs.get('class'))
#方式二
print(obj.get('class'))
#方式三
print(obj['class'])執(zhí)行結(jié)果:
['c1']
['c1']
['c1']
3、BS4具體使用
代碼實(shí)例:獲取所有飲品的名稱·
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)文章
關(guān)于python簡(jiǎn)單的爬蟲操作(requests和etree)
這篇文章主要介紹了關(guān)于python簡(jiǎn)單的爬蟲操作(requests和etree),文中提供了實(shí)現(xiàn)代碼,需要的朋友可以參考下2023-04-04
學(xué)python需要去培訓(xùn)機(jī)構(gòu)嗎
在本篇文章里小編給大家整理的是關(guān)于學(xué)python是否需要去培訓(xùn)機(jī)構(gòu)的相關(guān)內(nèi)容,有需要的朋友們可以閱讀下。2020-07-07
使用python和Django完成博客數(shù)據(jù)庫(kù)的遷移方法
下面小編就為大家分享一篇使用python和Django完成博客數(shù)據(jù)庫(kù)的遷移方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
PyCharm更換pip源、模塊安裝以及PyCharm依賴包導(dǎo)入導(dǎo)出功能
這篇文章主要給大家介紹了關(guān)于PyCharm更換pip源、模塊安裝以及PyCharm依賴包導(dǎo)入導(dǎo)出功能的相關(guān)資料,我們?cè)谑褂胮ycharm的時(shí)候,pycharm中的虛擬環(huán)境依賴包需要導(dǎo)出成一個(gè)文件,需要的朋友可以參考下2023-11-11
解決Python下json.loads()中文字符出錯(cuò)的問題
今天小編就為大家分享一篇解決Python下json.loads()中文字符出錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Django unittest 設(shè)置跳過某些case的方法
今天小編就為大家分享一篇Django unittest 設(shè)置跳過某些case的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12

