Suraj Naik
Suraj Naik

Reputation: 199

How to update HTML file using Python?

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

Upvotes: 1

Views: 2170

Answers (1)

SirJoe
SirJoe

Reputation: 74

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))

Upvotes: 1

Related Questions