首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python not浏览器未打开浏览器

Python not浏览器未打开浏览器
EN

Stack Overflow用户
提问于 2016-12-04 03:05:42
回答 1查看 755关注 0票数 0

我试图让自己熟悉Python与web发布的一个主要关注点,所以我环顾四周,找到了以下示例。在Windows7的PyScripter中运行2.7后,浏览器并没有像我预期的那样打开。代码出现在Notepad++中,显然是因为html后缀与记事本相关联。我尝试了大约12种不同的代码排列,但html文件仍然在记事本中打开,直到我将该文件与Firefox关联。当我包含print webbrowser._browsers命令时,我得到{'windows-default':,None,'c:\program files (x86)\internet explorer\iexplore.exe':None,}

这对我来说意味着IE应该是被调用的默认浏览器,但显然它不是。我是一个Python新手,有人能在这里给我一些启发吗?

代码语言:javascript
复制
'''A simple program to create an html file froma given string,
and call the default web browser to display the file.'''

contents = '''<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="content-type">
  <title>Hello</title>
</head>
<body>
Hello, World!
</body>
</html>
'''

import webbrowser

def main():
    browseLocal(contents)

def strToFile(text, filename):
    """Write a file with the given name and the given text."""
    output = open(filename,"w")
    output.write(text)
    output.close()

def browseLocal(webpageText, filename='C:\\Python27\\Programs\\tempBrowseLocal.html'):
    '''Start your webbrowser on a local file containing the text
    with given filename.'''
    strToFile(webpageText, filename)
    print webbrowser._browsers
    webbrowser.open(filename)


main()
EN

回答 1

Stack Overflow用户

发布于 2016-12-04 03:15:26

代码语言:javascript
复制
def browseLocal(webpageText):#take filename out of here
    '''Start your webbrowser on a local file containing the text
    with given filename.'''
    #define filename here.
    filename='C:\\Python27\\Programs\\tempBrowseLocal.html'
    strToFile(webpageText, filename)
    print webbrowser._browsers
    webbrowser.open(filename)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40951429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档