I have a simple test.html with html content and I need a way to insert this text:
"bla bla bla..."
in this tag:
<p id="thislist"> </p>
Is there a way to do that ?
I have a simple test.html with html content and I need a way to insert this text:
"bla bla bla..."
in this tag:
<p id="thislist"> </p>
Is there a way to do that ?
Maybe try Beautifulsoup
?
from bs4 import BeautifulSoup
with open('test.html') as f:
html = f.read()
soup = BeautifulSoup(html, "html.parser")
tag = soup.find('p', {'id': "thislist"})
tag.insert(0, "bla bla bla...")
Match it using a regular expression and insert the content within it:
import re
some_string = 'puppies kittens <p id="thislist"> </p> ponies'
print re.sub(
r'(<p id="thislist">)\s*(</p>)', r'\1bla bla bla...\2', some_string)
The output is
puppies kittens <p id="thislist">bla bla bla...</p> ponies
ponies instead of puppies kittens
bla bla bla...
ponies – xRobot Dec 11 '15 at 10:35