1

Could you please help me out in updating the title section below from "Test App Name" to "Demo App" using Python?

<!DOCTYPE html>
<html lang="en" class="selector-17-1">
<head class="selector-17-2">
  <title class="selector-17-4">Test App Name</title>
</head>

Thanks

Suraj Naik
  • 189
  • 2
  • 11

1 Answers1

1

This should work

first install dependencies:

pip install lxml
pip install beautifulsoup4

execute

from bs4 import BeautifulSoup

with open("yourfile.html", "r") as f:
    text = f.read()

soup = BeautifulSoup("lxml", text)
soup.title = "Demo App"

with open("mynewfile.html", "w") as f:
    f.write(str(soup))
SirJoe
  • 74
  • 7
  • Sorry. doesn't work – Suraj Naik Apr 26 '22 at 08:49
  • #This worked for me from bs4 import BeautifulSoup #update index.html file with open("index.html", "r") as inputFile: soup = BeautifulSoup(inputFile, 'html.parser') soup.title.string = "Demo App" with open("indexOut.html", "w") as outFile: outFile.write(str(soup)) – Suraj Naik Apr 26 '22 at 13:44