簡體   English   中英

Beautiful Soup 在TD旁邊找個class

[英]Beautiful Soup find a class next to a TD

這里的代碼


<tr class="even">
<td>**keyword** </td>
<td class="separator"></td>
<td>
<span title="blablabla" class="data-siloing">**Text I want**</span> </td>
</tr>

如何使用 beautifulsoup 獲取此文本?

TD知道后嘗試獲取span標簽與class之間的文本?

你可以試試:

from bs4 import BeautifulSoup

html_text = '''\
<tr class="even">
<td>**keyword** </td>
<td class="separator"></td>
<td>
<span title="blablabla" class="data-siloing">**Text I want**</span> </td>
</tr>'''

soup = BeautifulSoup(html_text, 'html.parser')

# 1. find the td with "keyword"
keyword_td = soup.find(lambda tag: tag.name == 'td' and 'keyword' in tag.text)

# 2. find the next span tag
span = keyword_td.find_next('span')

# 3. get the text
print(span.text.strip())

印刷:

**Text I want**

暫無
暫無

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

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