2014-08-28 53 views
0

問題是: 我有我的@ app.route和相對def(),其中顯示從「http://annotaria.web.cs.unibo.it/documents/」取自url的列表。 如何以html格式顯示此網址?當我點擊http://localhost:5000/articoli我想顯示我的網址列表。顯示元素在與燒瓶和BeautifulSoup 3和Python的HTML 2.7.8

非常感謝您

@app.route('/articoli', methods=['GET']) 
def lista_articoli(): 
lista = [] 
import urllib2 
from bs4 import BeautifulSoup 
url = urllib2.urlopen`http://annotaria.web.cs.unibo.it/documents/.read()` 
soup = BeautifulSoup(url) 
for row in soup.findAll('a'): 
    if row.parent.name == 'td': 
     if row["href"] : 
      myArticle = row["href"] 
      if '.html' in myArticle: 
       print myArticle 
       lista.append({'url':myArticle}) } 
+0

這很不清楚。爲什麼顯示的網址列表與您在燒瓶中生成的任何其他文檔不同? – 2014-08-28 17:33:43

+0

是的,我希望通過瓶子生成的HTML文檔與這些網址列表。 – 2014-08-28 19:58:11

+0

而且,你的問題到底是什麼?你知道如何用Flask正常創建頁面嗎? – 2014-08-28 20:31:58

回答

0

要做到這一點,有你的函數返回render_template方法,對此你會通過你的URL列表。

在你的函數結束時,嘗試:

render_template("articoli.html", lista = lista) 

你必須保存在模板的相應articoli.html模板文件夾,這反過來應該在同一位置的Python腳本。在模板的HTML中,您需要指定Flask/Jinja放置您提供的URL列表的位置,在這種情況下,這將是{{lista}}

這在Flask documentation中概述。

+0

Thomas,非常感謝! – 2014-08-29 12:45:59