When there are tags within other tags (e.g., the
<p>), the string element of the parent element is empty, and the strings element is a generator which produces all the strings.
<p> First p <b> First b </b>second part first p</p>
<p> Second p <a> first link</a> second part second p <a> second link</a> third part second p</p>
In my code,
soup = BeautifulSoup(html)#text above
ps = soup.find_all('p')
p0 = ps
for s in p0.strings:
#makes sure that child elements inside <p> tag are skipped
if s.findParent() == p0:
However, when I run this, I get
Traceback (most recent call last):
File "<pyshell#243>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/bs4/element.py", line 211, in replace_with
my_index = self.parent.index(self)
AttributeError: 'NoneType' object has no attribute 'index'
The first string of p0 had its text changed, but the last element did not, since an error was thrown. The same thing happens with the second element of
p1 = ps. How do I go about modifying each of the string elements separately? I want to preserve all of the existing tags.