假设我有以下html:
<div id='0'>
stuff here
</div>
<div id='1'>
stuff here
</div>
<div id='2'>
stuff here
</div>
<div id='3'>
stuff here
</div>
有没有一种简单的方法可以使用BeautifulSoup提取div
具有属性的所有,而与属性id
的值无关?我意识到用xpath做到这一点是微不足道的,但是在BeautifulSoup中似乎没有办法进行xpath搜索。
用于id=True
仅匹配具有属性集的元素:
soup.find_all('div', id=True)
反之亦然。您可以使用以下属性排除标签id
:
soup.find_all('div', id=False):
要查找具有给定属性的标签,您还可以使用CSS选择器:
soup.select('div[id]'):
但是不幸的是,这不支持搜索逆运算符的运算符。
演示:
>>> from bs4 import BeautifulSoup
>>> sample = '''\
... <div id="id1">This has an id</div>
... <div>This has none</div>
... <div id="id2">This one has an id too</div>
... <div>But this one has no clue (or id)</div>
... '''
>>> soup = BeautifulSoup(sample)
>>> soup.find_all('div', id=True)
[<div id="id1">This has an id</div>, <div id="id2">This one has an id too</div>]
>>> soup.find_all('div', id=False)
[<div>This has none</div>, <div>But this one has no clue (or id)</div>]
>>> soup.select('div[id]')
[<div id="id1">This has an id</div>, <div id="id2">This one has an id too</div>]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句