Without an example, it is difficult to know what you are doing, but here are two examples and they both seem to work fine:
from bs4 import BeautifulSoup, Tag
from bs4.element import soupsieve
MARKUP = """
<div class="a">
<div class="b">
<div class="c">
<div class="d">
</div>
</div>
</div>
</div>
"""
print('===== Test 1 =====')
soup1 = BeautifulSoup(MARKUP, 'html5lib')
soup1.select_one('div.c').replace_with('test')
print('--- Is it still there? ---')
print(soup1.select_one('div.c') is not None)
print('--- Result ---')
print(soup1)
print('\n\n===== Test 2 =====')
soup2 = BeautifulSoup(MARKUP, 'html5lib')
is_c = soupsieve.compile('div.c')
for el in soup2.descendants:
if isinstance(el, Tag) and is_c.match(el):
el.replace_with('test')
print('--- Is it still there? ---')
print(soup2.select_one('div.c') is not None)
print('--- Result ---')
print(soup2)
Output
===== Test 1 =====
--- Is it still there? ---
False
--- Result ---
<html><head></head><body><div class="a">
<div class="b">
test
</div>
</div>
</body></html>
===== Test 2 =====
--- Is it still there? ---
False
--- Result ---
<html><head></head><body><div class="a">
<div class="b">
test
</div>
</div>
</body></html>
Now, should you be modifying a document tree while in the middle of looping? Probably not as you are updating the loop items while you are looping through them. If I had to guess, this is why you are running into issues.
--
You received this message because you are subscribed to a topic in the Google Groups "beautifulsoup" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beautifulsoup/XmPZNGx2vu8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beautifulsou...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beautifulsoup/b0c374dd-ecf0-4f67-96fd-a31f01094120n%40googlegroups.com.