代码:
from bs4 import BeautifulSoup
soup = BeautifulSoup('<div><p>p_string</p><div>div_string</div></div>')
for m in soup.div:
print "extract(first loop): ", m.extract()
print "current soup.div(frist loop): ", soup.div #it contains another div block
print '___________________________________________________________'
#I have to do another for loop to purge the remaining div block, why?
for m in soup.div:
print "extract(second loop): ", m.extract()
print "current soup.div(second loop): ", soup.div #removed
结果:
extract(first loop): <p>p_string</p>
current soup.div(frist loop): <div><div>div_string</div></div>
___________________________________________________________
extract(second loop): <div>div_string</div>
current soup.div(second loop): <div></div>
为什么它没有在第一个p
循环中提取所有元素(div
和for
)?
答案 0 :(得分:1)
这是因为您在循环中调用extract()
,从树中删除标记 - 在迭代标记的子项时删除它们。它与iterating over the list and remove items from it in the loop基本相同。
相反,请使用.find_all()
:
for m in soup.div.find_all():
print m.extract()