39

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
Danielo515
  • 5,996
  • 4
  • 32
  • 66
  • 1
    Possible duplicate of [webbrowser.open() in python](http://stackoverflow.com/questions/22004498/webbrowser-open-in-python) – Jace Browning Apr 14 '17 at 21:00

8 Answers8

55

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)
  • 2
    :- 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 –  Dec 01 '16 at 08:34
  • 1
    the 2nd example doesn't work, just us the full path to the `html` file without the `file://` prefix. – Pedro Lobito Jun 08 '20 at 23:43
  • 2
    To open a URL in a new tab also `webbrowser.open_new_tab(url)` could be used. – and1er Feb 01 '21 at 10:47
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
10
import os
os.system("start [your's_url]")

Enjoy!

Yuval Pruss
  • 8,716
  • 15
  • 42
  • 67
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')
WyattBlue
  • 591
  • 1
  • 5
  • 21
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()
Rainmaker
  • 10,294
  • 9
  • 54
  • 89
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"))
snwflk
  • 3,341
  • 4
  • 25
  • 37
Goutam
  • 377
  • 1
  • 2
  • 11
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:)

PouriaDiesel
  • 660
  • 8
  • 11
  • 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. – Elis Byberi Mar 03 '20 at 21:20
  • 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. – PouriaDiesel Mar 04 '20 at 10:14
0
import os
os.system('open "/Applications/Safari.app" '+ '"' + os.path.realpath(fname)+ '"')
Sergey Zaitsev
  • 555
  • 5
  • 6
  • 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. – Danoram Nov 19 '20 at 16:42