如何在Python中打开浏览器中的HTML文件?

时间:2016-12-01 08:21:18

标签: javascript python html

我正在尝试从Python打开HTML文件,但我的脚本只是在Python中显示HTML文件的内容,而不是在浏览器中打开它。我该如何解决这个问题?如何在Chrome浏览器中打开HTML文件?

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脚本:

import urllib
page =  urllib.urlopen('testdata.html').read()
print page

8 个答案:

答案 0 :(得分:7)

尝试指定&#34;文件://&#34;在URL的开头。

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

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

或者

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)

答案 1 :(得分:5)

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

享受!

答案 2 :(得分:4)

您可以使用 webbrowser 库:

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

答案 3 :(得分:1)

您可以使用Selenium。

下载最新的chromedriver,将chromedriver.exe粘贴到&#34; C:\ Python27 \ Scripts&#34;。

然后

from selenium import webdriver

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

答案 4 :(得分:1)

这里不需要外部库,也可以处理本地文件。

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

答案 5 :(得分:0)

您可以从here下载最新版本的“ gecodriver”。然后将gecodriver可执行文件添加到您的项目中。然后pip install selenium和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')

希望我能帮助您:)

答案 6 :(得分:0)

我认为这是最简单的解决方案:

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

答案 7 :(得分:0)

import os
os.system('open "/Applications/Safari.app" '+ '"' + os.path.realpath(fname)+ '"')