Skip to content

Instantly share code, notes, and snippets.

@c0nstantine
Last active April 13, 2017 22:01
Show Gist options
  • Save c0nstantine/6456660ed181c37633cc47925b55c060 to your computer and use it in GitHub Desktop.
Save c0nstantine/6456660ed181c37633cc47925b55c060 to your computer and use it in GitHub Desktop.
>>> hr = BeautifulSoup('<p>first</p><hr><p>second</p><p>third</p>', 'html.parser')
>>> hr2 = BeautifulSoup('<p>first</p><hr/><p>second</p><p>third</p>', 'html.parser')
>>>
>>> for p in hr.find_all('p'):
... p.insert_after(hr.new_string('<div class="ad"></div>'))
...
>>> for p in hr2.find_all('p'):
... p.insert_after(hr2.new_string('<div class="ad"></div>'))
...
>>>
>>> hr.contents
[<p>first</p>, u'<div class="ad"></div>', <hr><p>second</p>&lt;div class="ad"&gt;&lt;/div&gt;<p>third</p>&lt;div class="ad"&gt;&lt;/div&gt;</hr>]
>>> hr2.contents
[<p>first</p>, u'<div class="ad"></div>', <hr/>, <p>second</p>, u'<div class="ad"></div>', <p>third</p>, u'<div class="ad"></div>']
>>>
===
>>> hr = BeautifulSoup('<p>first</p><hr><p>second</p><p>third</p>', 'html.parser')
>>> hr2 = BeautifulSoup('<p>first</p><hr/><p>second</p><p>third</p>', 'html.parser')
>>>
>>> hr
<p>first</p><hr><p>second</p><p>third</p></hr>
>>> hr2
<p>first</p><hr/><p>second</p><p>third</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment