Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit and create HTML file using Python

I am currently working on an assignment for creating an HTML file using python. I understand how to read an HTML file into python and then edit and save it.

table_file = open('abhi.html', 'w')
table_file.write('<!DOCTYPE html><html><body>')
table_file.close()

The problem with the above piece is it's just replacing the whole HTML file and putting the string inside write(). How can I edit the file and the same time keep it's content intact. I mean, writing something like this, but inside the body tags

<link rel="icon" type="image/png" href="img/tor.png">

I need the link to automatically go in between the opening and closing body tags.

like image 884
Lilly123 Avatar asked Feb 12 '16 05:02

Lilly123


People also ask

Can I edit HTML with Python?

Python library is a stand-alone solution to edit HTML that doesn't rely on other software.

Can Python generate HTML?

Python does not come with tools to generate HTML. If you want an advanced framework for structured HTML generation, I recommend Robin Friedrich's HTMLGen 2.2 (available at http://starship.python.net/crew/friedrich/HTMLgen/html/main.html), but I do not cover the package in this book.

How to generate HTML from Python program?

Save the above program as write-html.py and execute it. Use File -> Open in your chosen text editor to open helloworld.html to verify that your program actually created the file. The content should look like this: HTML Source Generated by Python Program. Now go to your Firefox browser and choose File -> New Tab, go to the tab, ...

How do I open an HTML file in Python?

Viewing the HTML source file In order to display the HTML file as a python output, we will be using the codecs library. This library is used to open files which have a certain encoding. It takes a parameter encoding which makes it different from the built-in open () function.

How to display HTML file as Python output?

In order to display the HTML file as a python output, we will be using the codecs library. This library is used to open files which have a certain encoding. It takes a parameter encoding which makes it different from the built-in open () function.

What is an HTML document in Python?

We’re going to be creating an HTML document using Python, so you will have to know what an HTML document is! One of the more powerful ideas in computer science is that a file that seems to contain code from one perspective can be seen as data from another. It is possible, in other words, to write programs that manipulate other programs.


1 Answers

You probably want to read up on BeautifulSoup:

import bs4

# load the file
with open("existing_file.html") as inf:
    txt = inf.read()
    soup = bs4.BeautifulSoup(txt)

# create new link
new_link = soup.new_tag("link", rel="icon", type="image/png", href="img/tor.png")
# insert it into the document
soup.head.append(new_link)

# save the file again
with open("existing_file.html", "w") as outf:
    outf.write(str(soup))

Given a file like

<html>
<head>
  <title>Test</title>
</head>
<body>
  <p>What's up, Doc?</p>
</body>
</html>  

this produces

<html>
<head>
<title>Test</title>
<link href="img/tor.png" rel="icon" type="image/png"/></head>
<body>
<p>What's up, Doc?</p>
</body>
</html> 

(note: it has munched the whitespace, but gotten the html structure correct).

like image 157
Hugh Bothwell Avatar answered Oct 23 '22 20:10

Hugh Bothwell