日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python?BautifulSoup?節點信息_python

作者:萬里顧一程 ? 更新時間: 2022-10-20 編程語言

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

欄目分類
最近更新