-2

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 ?

xRobot
  • 25,579
  • 69
  • 184
  • 304

2 Answers2

2

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...")
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
0

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
John Hoffman
  • 17,857
  • 20
  • 58
  • 81