How to open an HTML file in the browser from Python?

Question:

I am trying to open an HTML file from Python but my script just displays the contents of the HTML file in Python instead of opening it in the browser. How can I fix this problem? How can I open the HTML file in my Chrome browser?

testdata.html

<div>
    <a href="https://plot.ly/user001/2/" target="_blank" title="Success vs Failure" style="display: block; text-align: center;"><img src="https://plot.ly/~user001/2.png" alt="Success vs Failure" style="max-width: 100%;width: 600px;"  width="600" onerror="this.onerror=null;this.src='https://plot.ly/404.png';" /></a>
    <script data-plotly="user001:2"  src="https://plot.ly/embed.js" async></script>
</div>

Python 2.7 script:

import urllib
page =  urllib.urlopen('testdata.html').read()
print page
Asked By: user7135817

||

Answers:

import os
os.system("start [your's_url]")

Enjoy!

Answered By: Yuval Pruss

Try specifying the “file://” at the start of the URL.

// Also, use the absolute path of the file:

webbrowser.open('file://' + os.path.realpath(filename))

Or

import webbrowser
new = 2 # open in a new tab, if possible

// open a public URL, in this case, the webbrowser docs
url = "http://docs.python.org/library/webbrowser.html"
webbrowser.open(url,new=new)

// open an HTML file on my own (Windows) computer
url = "file://d/testdata.html"
webbrowser.open(url,new=new)
Answered By: user3146115

You can use webbrowser library:

import webbrowser
url = 'file:///path/to/your/file/testdata.html'
webbrowser.open(url, new=2)  # open in new tab
Answered By: Vasilii Medvedev

You can use Selenium.

download the latest chromedriver, paste the chromedriver.exe in “C:Python27Scripts”.

then

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("your page path")
print driver.page_source.encode('utf-8')
driver.quit()
display.stop()
Answered By: Rainmaker

you can download latest version of “gecodriver” from here.then add gecodriver executable file to your project.then pip install selenium and below the code for windows:

from selenium import webdriver   
from selenium.webdriver.firefox.options import Options   
import os

#optional
options = Options()   
options.set_preference('permissions.default.image', 2)   
options.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', False)   

#for windows
Driver = webdriver.Firefox(options=options, executable_path='geckodriver.exe')   
Driver.implicitly_wait(15)

#path of your project -> reference : "https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure/40227116"   
Root = os.path.dirname(os.path.abspath(__file__))    
driver.get('file://' + Root + 'path/to/htmlfile')

Hope I Helped You:)

Answered By: PouriaDiesel

Here’s a way that doesn’t require external libraries and that can work of local files as well.

import subprocess
import os

url = "https://stackoverflow.com"
# or a file on your computer
# url = "/Users/yourusername/Desktop/index.html
try: # should work on Windows
    os.startfile(url)
except AttributeError:
    try: # should work on MacOS and most linux versions
        subprocess.call(['open', url])
    except:
        print('Could not open URL')
Answered By: WyattBlue

I feel this is the easiest solution:

import os

os.getcwd() #To check the current working directory or path
os.chdir("D:\Folder Name\") # D:Folder Name is the new path where you want to save the converted dataframe(df) to .html file

import webbrowser

df.to_html("filename.html") #Converting dataframe df to html and saving with a name 'filename' and 
webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s").open("file://" + os.path.realpath("filename.html"))
Answered By: Goutam
import os
os.system('open "/Applications/Safari.app" '+ '"' + os.path.realpath(fname)+ '"')
Answered By: Sergey Zaitsev
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.