我试图获取的html:
<div id="unitType">
<h2>BB100 <br>v1.4.3</h2>
</div>
我有下面一个h2
标记的内容:
initialPage = beautifulSoup(urllib.urlopen(url).read(), 'html.parser')
deviceInfo = initialPage.find('div', {'id': 'unitType'}).h2.contents
print('Device Info: ', deviceInfo)
for i in deviceInfo:
print i
哪些输出:
('Device Info: ', [u'BB100 ', <br>v1.4.3</br>])
BB100
<br>v1.4.3</br>
如何使用BeautifulSoup而不是regex移除<h2>
、</h2>
、<br>
和</br>
html标记?我试过i.decompose()
和i.strip()
但都没用。它会抛出'NoneType' object is not callable
。
只需使用find andextractthebr标记:
或者使用replace-with将标记替换为文本:
要删除h2并保留文本:
如果你只想
"v1.4.3"
和"BB10"
,有很多方法可以和他们打交道:为什么你最后会收到短信
您可以检查元素是否是带有
if i.name == 'br'
的<br>
标记,然后只需将列表更改为包含内容。如果需要多次迭代,请修改列表。
相关问题 更多 >
编程相关推荐