46

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
1

8 Answers 8

69

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)
Sign up to request clarification or add additional context in comments.

4 Comments

:- import webbrowser new = 2 # open in a new tab, if possible url = "file://C:/Users/S/Desktop/Python/testdata.html" webbrowser.open(url,new=new). It just opens the notepad file
the 2nd example doesn't work, just us the full path to the html file without the file:// prefix.
To open a URL in a new tab also webbrowser.open_new_tab(url) could be used.
To get an URL from a file path you can use Path.as_uri e.g. Path(filename).as_uri(). That will handle any OS differences.
12
import os
os.system("start [your's_url]")

Enjoy!

2 Comments

:- Is there a way to open the local html document instead of displaying it on plotly webpage?
Check below answer #Nayan Godhani
11

You can use webbrowser library:

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

Comments

4

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

Comments

2

You can use Selenium.

download the latest chromedriver, paste the chromedriver.exe in "C:\Python27\Scripts".

then

from selenium import webdriver

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

Comments

1

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

Comments

0

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

2 Comments

It is very kind of you to post this tutorial about selenium usage. However, the user just wants to open a browser when he clicks a link, like in a pdf (or word) document.
I know but I want Anyone who uses selenium, whether professional or beginner,by seeing this post,use it without any problem.for e.g I used it to create machine intelligence scrapper but this user can delete options and Root part.
0
import os
os.system('open "/Applications/Safari.app" '+ '"' + os.path.realpath(fname)+ '"')

1 Comment

This answer has tremendous value to people who find this question but only those who intend to open the file using Safari from a mac as 'open' is a unix command. Could you include a comment stating that and also include an example definition for fname. Keep up the good work.