grt
grt

Reputation: 23

python3 soup,replace html element content and save to file

how to replace text content of html tag in file and save them to another(some), file ?

Ex. there is a file index.html

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <p itemprop="someprop">SOME BIG TEXT</p>
    </body>
</html>

I need to replace the text "SOME BIG TEXT" in the "p" tag to "ANOTHER BIG TEXT"

from bs4 import BeautifulSoup

with open("index.html","r") as file:
 fcontent=file.read()
 sp=BeautifulSoup(fcontent,'lxml')
 t='new_text_for_replacement'

 print(sp.replace(sp.find(itemprop="someprop").text,t))

What am I doing wrong ?

Thank you

Upvotes: 2

Views: 1215

Answers (2)

H&#229;ken Lid
H&#229;ken Lid

Reputation: 23064

Use open() on the output file to write to it.

with open('index.html', 'r') as file:
    fcontent = file.read()

sp = BeautifulSoup(fcontent, 'html.parser')

t = 'new_text_for_replacement'

# replace the paragraph using `replace_with` method
sp.find(itemprop='someprop').replace_with(t)

# open another file for writing
with open('output.html', 'w') as fp:
    # write the current soup content
    fp.write(sp.prettify())

If you want to replace just the inner content of the paragraph instead of the paragraph element itself, you can set the .string property.

sp.find(itemprop='someprop').string = t

Upvotes: 1

Nazim Kerimbekov
Nazim Kerimbekov

Reputation: 4783

The problem relies upon on the way you are searching for the criteria try changing the following code:

 print(sp.replace(sp.find(itemprop="someprop").text,t))

to this:

 print(sp.replace(sp.find({"itemprop":"someprop"}).text,t))

hopefully, this helps

(PS: based of your questionI'm assuming that you only have one thing to replace)

Upvotes: 0

Related Questions