Search code examples
pythonhtmlpywin32notepad

HTML Source to Notepad File


how can i use python to save a source code i got from a site and then save it as a .html or .txt file for a project im working on

im using something like this to get the code

from PAM30 import PAMIE
import win32com.client 
ie =PAMIE() 
website="http://example.url"
ie.navigate(website)
ie.setTextBox("account","my_user")
ie.setTextBox("pw","my_pass")
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys("{ENTER}", 0)
data = ie.outerHTML()

print(data)

Solution

  • from PAM30 import PAMIE
    import win32com.client 
    ie =PAMIE() 
    website="http://example.url"
    ie.navigate(website)
    ie.setTextBox("account","my_user")
    ie.setTextBox("pw","my_pass")
    shell = win32com.client.Dispatch("WScript.Shell")
    shell.SendKeys("{ENTER}", 0)
    data = ie.outerHTML()
    
    with open("out.txt", "w") as f:
        f.write(data)
    

    saves to 'out.txt'. you can change it, too.

    for unicode errors, try codecs module.

    import codecs
    
    with codecs.open("out.html", "w", encoding="utf-8") as f:
        f.write(data)