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

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -