python - How to find & change texts outside of tags with Beautiful Soup? -
i have file this:
words1 outside of tag <tag1> words2 inside of tag1 </tag1> words3 outside of tag
i want extract strings outside of tag1
, change beautifulsoup:
changed word1 <tag1> words2 inside of tag1 </tag1> changed word3
how can replace words out of tags beautifulsoup ?
text elements considered child elements of parent element.
if find tag1
, can find text before , after in attributes .previoussibling
, .nextsibling
. or, can find parent tag, , select appropriate children.
example:
from bs4 import beautifulsoup # assuming beautifulsoup 4 doc = """ words1 outside of tag <tag1>words2 inside of tag1</tag1> words3 outside of tag """ soup = beautifulsoup(doc, 'html.parser') tag = soup.find('tag1') tag.previoussibling.replacewith('changed word1 ') tag.nextsibling.replacewith(' changed word3') print(soup)
Comments
Post a Comment