Reputation:
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
Upvotes: 46
Views: 102885
Reputation: 423
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"))
Upvotes: 1
Reputation: 585
import os
os.system('open "/Applications/Safari.app" '+ '"' + os.path.realpath(fname)+ '"')
Upvotes: 0
Reputation: 631
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')
Upvotes: 4
Reputation: 735
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:)
Upvotes: 0
Reputation: 11090
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()
Upvotes: 2
Reputation:
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)
Upvotes: 66
Reputation: 170
You can use webbrowser library:
import webbrowser
url = 'file:///path/to/your/file/testdata.html'
webbrowser.open(url, new=2) # open in new tab
Upvotes: 11