Quote:
Originally Posted by TonytheBookworm
In that case you would probably wanna do a preprocess_html and insert a <br> somehow another. I haven't mastered the inserting part yet 
|
Have you ever seen that puzzle with 3 posts and disks of increasing size? Playing with new tags, inserting tags, moving tags, etc. is like that puzzle.
You might want to review my mods to your Buckmaster recipe here:
https://www.mobileread.com/forums/sho...postcount=2651
It adds <p></p> surrounding images (instead of inserting a <br>).
Beautiful Soup lets you create tags easily. It lets you replace one tag with another (replaceWith). It lets you insert a tag into another tag, but it requires you to insert by location number. The problem is that you either have to calculate the correct location number, or you have to replace a tag with a newly created tag. The first is a pain. The second is tricky if you want to keep the contents of the tag being replaced.
One approach can be seen here:
Code:
for img_tag in soup.findAll('img'):
parent_tag = img_tag.parent
if parent_tag.name == 'a':
new_tag = Tag(soup,'p')
new_tag.insert(0,img_tag)
#at this point img_tag has been extracted from soup
#and put into new_tag - parent_tag remains in soup
parent_tag.replaceWith(new_tag)