如何获取“ alt”的所有内容?

时间:2019-10-11 20:21:14

标签: python beautifulsoup

我正在寻找剪贴文本html 这是我要抓取的html文本,尤其是我要恢复的内容是属性或包含在“ alt”中?

html_text = """
<td class="kx max" onclick="e_t.track_click('lo-button-click', 'block-1x2_ft_2');"><span class=" number-wrap  down" alt="4.10[d]3.80" eu="4.10[d]3.80">3.80</span></td>
"""

这就是我所做的

print(soup.find_all('span','alt'))

我希望输出为"4.10 [d] 3.80"

1 个答案:

答案 0 :(得分:0)

使用tag['alt']

print(soup.select_one('.number-wrap.down')['alt'])

要获取多个物品。

for item in soup.select('.number-wrap.down'):
    print(item['alt'])

import bs4
html_text = """
<td class="kx max" onclick="e_t.track_click('lo-button-click', 'block-1x2_ft_2');"><span class=" number-wrap  down" alt="4.10[d]3.80" eu="4.10[d]3.80">3.80</span></td>
<td class="kx max" onclick="e_t.track_click('lo-button-click', 'block-1x2_ft_2');">
<span class=" number-wrap  down" alt="4.10[d]3.80" eu="4.10[d]3.80">3.80</span></td>
"""
soup=bs4.BeautifulSoup(html_text,'html.parser')
for item in soup.select('.number-wrap.down'):
    print(item['alt'])