19

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.

2
  • 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) Commented 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 it Commented Feb 12, 2016 at 6:05

1 Answer 1

39

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

Sign up to request clarification or add additional context in comments.

1 Comment

Try using outf.write(bs4.BeautifulSoup.prettify(soup)) it fixes the whitespace issue Was going to put up a separate answer but credit where credit is due. Read the BS documentation on attributes and changing tag names - super helpful

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.