簡體   English   中英

如何在Python中打開HTML文件

[英]How do I open an HTML file in Python

我嘗試了各種不同的方法來從python代碼打開HTML文件,但是每次出現“ 500內部服務器錯誤”時,

這是我的python腳本:

    if (variable == "0, User and IP logged"): #conditional to check if user's credentials were accepted by the API
        page = urllib.urlopen("mainPage.html").read()
        print page
        #html file is opened and read - NOT WORKING!

這是我的html文件:

    <html>
    <header><title>This is title</title></header>
    <body>
    Hello world
    </body>
    </html>

如何獲取python腳本來顯示我的世界頁面? 我的導師說我應該使用open(),但是我不確定如何使用。

試試這個...

webbrowser.open_new_tab('helloworld.html')

首先,您需要運行將為網頁提供服務器的Web服務器:

在這里我正在使用示例瓶子Web框架

bottle_server.py

from bottle import route, run

@route('/')
def dashboard():
    return '<b>This is index page</b>'

run(host='localhost', port=8080)

使用運行腳本

python bottle_server.py

現在,服務器正在端口8080的本地主機上運行

這是示例python腳本

客戶端

import urllib
print urllib.urlopen('http://localhost:8080/").read()

使用運行客戶端腳本

python Client.py

這將產生如下輸出

<b>This is index page</b>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM