【发布时间】:2017-09-21 16:49:47
【问题描述】:
我有以下 HTML:
<div>
<h5>Item1</h5>
$14.00<br>
<br>
<h5>Item2</h5>
$16.29 (Shop Rite)<br>
$15.49 (Costco)<br>
<br>
<h5>Item3</h5>
...
</div>
我正在尝试根据项目编号将此信息组织到一个列表中,如下所示:
+--------+--------------------+
| Item1 | $14.00 (BJs) |
| Item2 | $16.29 (Shop Rite) |
| Item2 | $15.49 (Costco) |
+--------+--------------------+
我想要类似以下的东西:
Items = []
if (BS.find('h5', text="Item1")):
for content in BS.find('h5', text="Item1").parent:
Price = BS.find('h5', text="Item1").parent.content[0]
Items.append("Item1", Price)
我的主要目标是能够单独获取由<br> 标签分隔的文本,然后将其存储到名为Items 的列表中,但我不确定如何遍历每个<br> 标签<div> 标签基于<h5> 标签。
【问题讨论】:
标签: python html text beautifulsoup parent-child