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.
-
I dn't know if I understand your assignment scope correctly, but I would advise you take a look at BeautifulSoup for this (crummy.com/software/BeautifulSoup)Obsidian– Obsidian2016年02月12日 06:03:37 +00:00Commented Feb 12, 2016 at 6:03
-
Mainly because with the approach you're taking, you can just put the whole html in a single doc string and then just write it into a file. Nothing programmatic about itObsidian– Obsidian2016年02月12日 06:05:10 +00:00Commented Feb 12, 2016 at 6:05
1 Answer 1
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).
1 Comment
Explore related questions
See similar questions with these tags.