2017-02-09 6 views
0

Powershellで出力を印刷できますが、PowerShellではなく、Webサイトにテキストを表示します。ここで ウェブサイトへのpython出力の表示

は私のindex.htmlです:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Fluxus Generator</title> 
</head> 

<body bgColor="peachpuff"> 
    <center><h1>Fluxus Generator</h1><center> 

    <center><img src="card.png" alt="Card" /></center> 
    <font face= "Calibri" size="7" color = "purple"> 

    <center><p><a href="/">Another Card</a></p></center> 
</body> 
</html> 

、ここapp.pyです:

from bs4 import BeautifulSoup 
import requests 
import random 

from flask import Flask 
from flask import render_template 

app = Flask(__name__) 

@app.route('/') 
def index(): 
    return render_template('index.html') 

def generate_word(): 
    """Generate a list of words and shuffle them""" 
    words = list() 

    req = requests.get('https://en.wikipedia.org/wiki/List_of_plain_English_words_and_phrases') 
    html_source = req.text 
    soup = BeautifulSoup(html_source, "html.parser") 

    for source in soup.find_all('a', { "class" : "extiw" }): 
     word = source.find(text = True) 
     try: 
      word = str(word) 
      words.append(word) 
     except: 
      pass 

    random.shuffle(words) 
    return words 


def generate_instruction(words): 
    """Build an instruction from the word lists""" 
    instruction_struct = [ 
     'I', words[0], words[1], words[8], 'the', 
     words[2], words[5], 'with', 
     words[4], words[7] 
    ] 

    instruction = ' '.join(instruction_struct) 
    print instruction 


words = generate_word() 
generate_instruction(words) 

if __name__ == '__main__': 
    app.run() 

I(私は別に動作しませんでしたので、main.pyでapp.pyユナイテッド) cgi、webを試しましたがうまくいきませんでした。 私のウェブサイトにテキストが表示されるようにコードを変更する必要はありますか? index.htmlに追加する必要がありますか?

+0

http://flask.pocoo.org/docs/0.12/quickstart/#rendering-templates –

+0

あなたが「のdidnをどういう意味ですか運動しない? – EJoshuaS

答えて

0

BeautifulSoupは間違ったツールです。これはページを解析するためのもので、必要なのは逆のページの作成です。

Jinja2(大)やTempita(小)などテンプレートエンジンが必要です。それによって

、あなたはその後、HTML テンプレート、正常な構造を持つ文書といくつかのプレースホルダ、データでプレースホルダを置き換えることにより、にそれをレンダリングを作成します。

(フラスコが良い選択である。さらに少ない依存関係の場合、web.pyを試してみてください。)

関連する問題