如何从 Python 在浏览器中打开 HTML 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40905703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open an HTML file in the browser from Python?
提问by
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?
我试图从 Python 打开一个 HTML 文件,但我的脚本只是在 Python 中显示 HTML 文件的内容,而不是在浏览器中打开它。我该如何解决这个问题?如何在 Chrome 浏览器中打开 HTML 文件?
testdata.html
测试数据.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:
Python 2.7 脚本:
import urllib
page = urllib.urlopen('testdata.html').read()
print page
回答by
Try specifying the "file://" at the start of the URL.
尝试在 URL 的开头指定“file://”。
// 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)
回答by Yuval Pruss
import os
os.system("start [your's_url]")
Enjoy!
享受!
回答by Vasilii Medvedev
You can use webbrowserlibrary:
您可以使用网络浏览器库:
import webbrowser
url = 'file:///path/to/your/file/testdata.html'
webbrowser.open(url, new=2) # open in new tab
回答by Rainmaker
You can use Selenium.
您可以使用硒。
download the latest chromedriver, paste the chromedriver.exe in "C:\Python27\Scripts".
下载最新的 chromedriver,将 chromedriver.exe 粘贴到“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()
回答by NaabNuts
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:
您可以从这里下载最新版本的“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')
Hope I Helped You:)
希望我帮助你:)
回答by WyattBlue
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')