Search code examples
pythonhtmlfile-writing

How to parse HTML and then write it to a .py file


I am trying to parse some HTML and then have that HTML written to a .py file. Here is the code I am using:

from html.parser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_data(self, data):
        print(data)
        f = open('/Users/austinhitt/Desktop/Test.py', 'w')
        f = open('/Users/austinhitt/Desktop/Test.py', 'r')
        t = f.read()
        f = open('/Users/austinhitt/Desktop/Test.py', 'w')
        f.write(t + '\n' + data)
        f.close()

parser = MyHTMLParser()
parser.feed('<html>'
            '<body>'
            '<p>import time as t</p>'
            '<p>from os import path</p>'
            '<p>import os</p>'
            '</body>'
            '</html>')

I am not getting any error, however only the contents of the last p tag are being put into the file. I only want what is inside of the p tags to be added to the file, not the p tag itself. I need the content of every p tag added to the file, and I don't want to use BeautifulSoup or other non-built in modules. I am using Python 3.5.1


Solution

  • It seems that you read file "Test.py" after use "write" mode, that may cause data lost.