我正在识别头的<strong>
标记。然而,每当我试图获取其余信息以将其标识为“info”时,我只会返回<em>Parade </em>
,而不是<p>
标记中的所有内容
这是我的密码:
<p><strong>High School Honors: </strong><em>Parade </em>All-American; <em>Chicago Sun-Times </em>Illinois Player of the Year honors; rushed for 2,100 yards and 31 TDs as a senior; led team to 14-0 record and Class 4A State Championship as a junior with 1,820 yards and 26 TDs; also lettered in baseball.</p>
for strong_tag in soup.find_all('strong'):
headers = strong_tag.text.replace(':', '').replace('\xa0', ' ').strip()
info = strong_tag.next_sibling
headerList.append(headers)
infoList.append(info)
print(headerList)
print(infoList)
我想这就是你要找的。它查找父p元素,将soup对象转换为字符串,删除strong元素,然后将字符串转换回soup对象
这将产生以下结果:
编辑
您也可以使用
contents
,但必须迭代所有NavigableStrings
:示例
输出
使用
get_text()
和split()
:示例
输出
相关问题 更多 >
编程相关推荐