2

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

4
  • Does it replace anything or virtually nothing? (Please check if it the first p or not) Commented Jun 18, 2018 at 23:12
  • there can be many tags "p" but "itemprop=someprop" is only one Commented Jun 18, 2018 at 23:18
  • I've added an answer, hope it helps :) Commented Jun 18, 2018 at 23:19
  • You mean using my answer? Commented Jun 18, 2018 at 23:23

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.