2014-08-28 51 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