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

如何从Python在浏览器中打开HTML文件?,python,html,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;">

我试图从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>
享受吧

尝试在URL开头指定“file://”

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

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

您可以使用库:

import webbrowser
url = 'file:///path/to/your/file/testdata.html'
webbrowser.open(url, new=2)  # open in new tab
你可以使用硒

下载最新的chromedriver,将chromedriver.exe粘贴到“C:\Python27\Scripts”中

然后


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

希望我能帮助你:)

这里有一种方法,它不需要外部库,也可以处理本地文件

导入子流程
导入操作系统
url=”https://stackoverflow.com"
#或者你电脑上的文件
#url=“/Users/yourusername/Desktop/index.html
try:#应该可以在Windows上工作
os.startfile(url)
除属性错误外:
try:#应适用于MacOS和大多数linux版本
子进程调用(['open',url])
除:
打印('无法打开URL')

我觉得这是最简单的解决方案:

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

:-有没有办法打开本地html文档而不是将其显示在绘图网页上?检查下面的答案#Nayan Godhani:-导入webbrowser new=2#在新选项卡中打开,如果可能的话,url=“file://C:/Users/S/Desktop/Python/testdata.html”webbrowser.open(url,new=new)。它只是打开记事本文件。第二个示例不起作用,只显示
html
文件的完整路径,而不带
文件://
前缀。要在新选项卡中打开URL,还需要
webbrowser。打开新选项卡(URL)
可以使用。如果您能发布有关selenium用法的本教程,您将不胜感激。但是,用户只想在单击链接时打开浏览器,如pdf(或word)中的链接文档。我知道,但我希望任何使用selenium的人,无论是专业人士还是初学者,只要看到这篇文章,就可以毫无问题地使用它。例如,我用它创建了机器智能刮板,但这个用户可以删除选项和根部分。这个答案对发现这个问题的人有巨大的价值,但只有那些打算打开文件的人在mac上使用Safari,因为“open”是一个unix命令。您是否可以添加一条注释,说明这一点,并为
fname
添加一个示例定义。请继续努力。
import webbrowser
url = 'file:///path/to/your/file/testdata.html'
webbrowser.open(url, new=2)  # open in new tab
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("your page path")
print driver.page_source.encode('utf-8')
driver.quit()
display.stop()
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')
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"))
import os
os.system('open "/Applications/Safari.app" '+ '"' + os.path.realpath(fname)+ '"')