Quote:
Originally Posted by clintiepoo
Guys, please help. How do I put spaces between the different tags I'm using? Right now, everything is stringing together in one big line. This can't be that hard.
|
Yes it can
Quote:
How it is:
dateIMAGEcaption
I want:
date
IMAGE
caption
|
Here's some code from a recipe to find images and put them inside a p tag. You will find lots of recipes where the img is on the same line as the next text to avoid having to deal with this issue.
Code:
def preprocess_html(self,soup):
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)
parent_tag.replaceWith(new_tag)
elif parent_tag.name == 'p':
if not self.tag_to_string(parent_tag) == '':
new_div = Tag(soup,'div')
new_tag = Tag(soup,'p')
new_tag.insert(0,img_tag)
parent_tag.replaceWith(new_div)
new_div.insert(0,new_tag)
new_div.insert(1,parent_tag)
return soup