我有一个html文件说:
<html>...
<li id="123"></li>
<li id="3455"></li>
....
</html>
如何使用BeautifulSoup在python中单独获取所有id的值?
所需的输出是:["123","3455"]
答案 0 :(得分:2)
要获取所需的列表,请使用list comprehension。它可以在一行中完成,如下所示(最后一行):
html = '<html> <li id="123"></li><li id="3455"></li> </html>'
soup = BeautifulSoup(html)
attrs = [li['id'] for li in soup.find_all('li')]
答案 1 :(得分:0)
from BeautifulSoup import BeautifulSoup
foo = '<html> <li id="123"> </li> <li id="3455"></li> </html>'
soup = BeautifulSoup(foo)
for id in soup.html.findAll('li'):
print id['id']