簡體   English   中英

使用 BeautifulSoup 從湯中提取標簽

[英]extract tags from soup with BeautifulSoup

在此處輸入圖像描述

'''
<div class="kt-post-card__body>
<div class="kt-post-card__title">Example_1</div>
<div class="kt-post-card__description">Example_2</div>
<div class="kt-post-card__bottom">
<span class="kt-post-card__bottom-description kt-text-truncate" title="Example_3">Example_4</span>
</div>
</div>
'''

根據我附上的圖片,我想提取所有“kt-post-card__body”屬性,然后從它們中提取:

("kt-post-card__title", "kt-post-card__description") 

像一個列表。

我試過這個:

ads = soup.find_all('div',{'class':'kt-post-card__body'})

但是使用ads[0].div我只能訪問"kt-post-card__title" ,而"kt-post-card__body"還有其他三個子標簽,例如: "kt-post-card__description""kt-post-card__bottom" 。 .. , 這是為什么?

嘗試這個:

ads = soup.find_all('div',{'class':'kt-post-card__body'})

ads[0]

我認為你只得到第一個 div 因為你調用ads[0].div

因為你的問題不是很清楚 - 提取類:

for e in soup.select('.kt-post-card__body'):
    print([c for t in e.find_all() for c in t.get('class')])

輸出:

['kt-post-card__title', 'kt-post-card__description', 'kt-post-card__bottom', 'kt-post-card__bottom-description', 'kt-text-truncate']

要獲取文本,您還必須迭代您的ResultSet並可以訪問每個元素文本以填充您的列表或使用stripped_strings

例子
from bs4 import BeautifulSoup

html_doc='''
<div class="kt-post-card__body">
<div class="kt-post-card__title">Example_1</div>
<div class="kt-post-card__description">Example_2</div>
<div class="kt-post-card__bottom">
<span class="kt-post-card__bottom-description kt-text-truncate" title="Example_3">Example_4</span>
</div>
</div>
'''

soup = BeautifulSoup(html_doc)

for e in soup.select('.kt-post-card__body'):
    data = [
        e.select_one('.kt-post-card__title').text,
        e.select_one('.kt-post-card__description').text      
    ]
    print(data)        

輸出:

['Example_1', 'Example_2']

或者

print(list(e.stripped_strings))

輸出:

['Example_1', 'Example_2', 'Example_4']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM