Python、BeautifulSoup - <div> 文本和 <img> 属性顺序正确

标签 python html beautifulsoup

我有一小段 HTML,我想使用 BeautifulSoup 运行它。我已经掌握了基本导航,但这个让我难住了。

这是一段 HTML 示例(完全是虚构的):

<div class="textbox">
    Buying this item will cost you 
    <img align="adsbottom" alt="1" src="/1.jpg;type=symbol"/>
    silver credits and
    <img align="adsbottom" alt="1" src="/1.jpg;type=symbol"/>
    golden credits
</div>

使用 img 标签的“alt”属性,我希望看到以下结果: 购买此元素将花费您 1 个银币和 1 个金币

我不知道如何按顺序循环 div 标签。我可以执行以下操作来提取 div 标签中包含的所有文本

html = BeautifulSoup(string)
print html.get_text()

获取 div 标签中包含的所有文本,但这会给我这样的结果: 购买此元素将花费您银币和金币

同样,我可以通过这样做从 img-tags 中获取 alt-attributes 的值:

html = BeautifulSoup(string).img
print html['alt']

当然这只会给我属性值。

如何以正确的顺序遍历所有这些元素?是否可以连续读取div元素中的文本和img元素的属性?

最佳答案

你可以遍历一个标签的所有子标签,包括文本;测试它们的类型以查看它们是 Tag 还是 NavigableString 对象:

from bs4 import Tag

result = []
for child in html.find('div', class_='textbox').children:
    if isinstance(child, Tag):
        result.append(child.get('alt', ''))
    else:
        result.append(child.strip())

print ' '.join(result)

演示:

>>> from bs4 import BeautifulSoup, Tag
>>> sample = '''\
... <div class="textbox">
...     Buying this item will cost you 
...     <img align="adsbottom" alt="1" src="/1.jpg;type=symbol"/>
...     silver credits and
...     <img align="adsbottom" alt="1" src="/1.jpg;type=symbol"/>
...     golden credits
... </div>
... '''
>>> html = BeautifulSoup(sample)
>>> result = []
>>> for child in html.find('div', class_='textbox').children:
...     if isinstance(child, Tag):
...         result.append(child.get('alt', ''))
...     else:
...         result.append(child.strip())
... 
>>> print ' '.join(result)
Buying this item will cost you 1 silver credits and 1 golden credits

关于Python、BeautifulSoup - <div> 文本和 <img> 属性顺序正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20590624/

相关文章:

python - 有一种通过按键检查 QCheckBox 的方法吗?

当服务器上的(子)目录在哪里时,Python ftp.nlst()返回空列表

javascript - 我如何实现自动建议,如浏览器搜索字段(不是自动完成下拉菜单)?

python - 如何在 Python 中循环访问 html-table-dataset

python - 如何在 Python 中从 <span class ="className">TEXT I WANT</span> 中获取文本

python - BeautifulSoup 中的逗号匹配

python - Mac 上的 Django 和 mysql

python - glance 将 Openstack 中的 Images 存储在哪里

javascript - 结果集如何在 JavaScript 中传递?

css - 在 Internet Explorer 9 中使用 CSS 对齐 Div 列表中的文本