python - How to add <br> tags with BeautifulSoup? -


so let's have

<p>hello world</p> 

can beautifulsoup add
tag so?

<br><p>hello world</p> 

initially around doing like:

soup = beautifulsoup("<p>hello world<p>") soup = beautifulsoup(re.compile('(<p>)', '<br>\1', soup.prettify()) 

but problem in actual usage more complex html .prettify() messes html adding whitespace , lines.

i checked docs doesn't mention

<br> 

tag @ all.

it can done using soup.insert() function

>>> br = soup.new_tag('br') >>> br <br/> >>> soup = beautifulsoup("<p>hello world</p>") >>> soup.insert(0,br) >>> soup <br/><p>hello world</p> 

the insert() function inserts tag @ numeric position. here have specified 0 inserted @ start.


Comments

Popular posts from this blog

java - Solr query version issue: Invalid version or the data in not in 'javabin' format -

Hard vs. Soft Water: What's The Difference?

The Ten Most Livable Cities In The World