最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python BautifulSoup 節(jié)點(diǎn)信息

 更新時(shí)間:2022年08月24日 14:19:42   作者:萬里顧一程  
這篇文章主要介紹了Python BautifulSoup 節(jié)點(diǎn)信息,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

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:對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:對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'
}
# 定制請求,發(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é)點(diǎn)信息的文章就介紹到這了,更多相關(guān)Python BautifulSoup 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

普兰店市| 揭东县| 天镇县| 法库县| 融水| 杭锦后旗| 绥阳县| 绥江县| 五华县| 酉阳| 牟定县| 韩城市| 滁州市| 郓城县| 和林格尔县| 花垣县| 兴隆县| 闽清县| 阜新市| 云林县| 电白县| 胶州市| 长治县| 福海县| 莱州市| 华阴市| 襄樊市| 历史| 永安市| 安仁县| 衡东县| 德化县| 都江堰市| 长岭县| 宜昌市| 保定市| 萍乡市| 安多县| 石渠县| 眉山市| 海伦市|