2016-12-03 49 views
0

私はPythonに慣れようとしていますが、Webパブリッシングに大きな懸念を抱いています。 Windows 7のPyScripterで2.7を実行しても、期待通りのブラウザが起動しなかった。このコードは、Notepad ++で出てきました.HTMLサフィックスがメモ帳に関連付けられていたためです。私はコードの約12の異なる置換を試しましたが、htmlファイルはFirefoxとそのファイルを関連付けるまでNotepadでまだ開いていました。 print webbrowser._browsersコマンドを含めると、{'windows-default':[、なし]、 'c:\ program files(x86)\ Internet Explorer \ iexplore.exe':[なし、]}Python webbrowserがブラウザを開けません

これはIEが呼び出されるデフォルトブラウザでなければならないことを意味しますが、明らかにそうではありません。私がPythonの初心者のように私にここで教えてもらえますか?

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

答えて

0
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

ファイル名の宣言が働いていない移動。 htmlファイルがメモ帳に関連付けられている場合、デフォルトのブラウザではなく、メモ帳が表示されます。 – bobv

+0

@Bobv webbrowserモジュールがローカルファイルを開かない可能性があります。ここで提案されているように、サブprocess.popen()を試してください。http://stackoverflow.com/questions/15054434/how-can-i-open-files-in-external-programs-in-python – mcmxl

+0

ありがとう、subprocess.Popen()働いているようだ。 – bobv

関連する問題