在Flask中打印元组列表

时间:2017-03-18 15:50:36

标签: python html flask beautifulsoup jinja2

我正在尝试将元组列表打印到我的html Flask模板中,但是我收到了这个错误:

self.name, self.filename)
jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'

我在浏览器中放置localhost地址时出现500内部服务器错误

如何将数据打印到浏览器上?

CODE: eslJobs.py

from flask import Flask, render_template    
import bs4 as bs
import urllib.request

app = Flask(__name__)

@app.route('/')
def chinaJobs():
    sauce = urllib.request.urlopen('http://www.eslcafe.com/jobs/china/').read()

    soup = bs.BeautifulSoup(sauce, 'html.parser')

    dl = soup.dl

    chinaAds = []
    china = []

    for words in dl.find_all('a'):
        links = words.get('href')
        link_text = words.text

        if 'university' in link_text.lower():
            chinaAds.append([links, link_text])

        if 'college' in link_text.lower():
            chinaAds.append([links, link_text])

    for ad in chinaAds:
        china.append(tuple(ad))
        return render_template("eslJobs.html", china=china)   

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

CODE: eslJobs.html

<!DOCTYPE html>
<html>
<head>
    <title>ESL Jobs</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bootstrap.min.css') }}">
</head>
<body>

<div class="page-header">
  <h1>College &amp; University ESL Jobs</h1>
</div>


<ul class="list-group">
{% for href, caption in {{ china }}: %}
  <li class="list-group-item"><a href="{{ href }}">{{ caption }}</a>  </li>
{% endfor %}
</ul>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

请参阅jinja document中的{ { title : "Adventures of Huckleberry Finn" author : ["Mark Twain", "Thomas Becker", "Colin Barling"], pageCount : 366, genre: ["satire"] , release: "1884", }, { title : "The Great Gatsby" author : ["F.Scott Fitzgerald"], pageCount : 443, genre: ["Novel, "Historical drama"] , release: "1924" }, { title : "This Side of Paradise" author : ["F.Scott Fitzgerald"], pageCount : 233, genre: ["Novel] , release: "1920" } } 用法。

1) Grab all books by "F.Scott Fitzgerald"
2) Grab books under genre "Novel"
3) Grab all book with page count less than 400
4) Grab books with page count more than 100 no later than 1930

因此,您在$( "div:contains('John')" ).css( "text-decoration", "underline" );内的模板中不需要双括号,您只需将其删除即可。

For

在你的python脚本中,有一个缩进错误,阻止你获得完整的列表。

<h1>Members</h1>
<ul>
{% for user in users %}
  <li>{{ user.username|e }}</li>
{% endfor %}
</ul>

答案 1 :(得分:1)

我认为你需要修改这一行:

{% for href, caption in {{ china }}: %}

{% for href, caption in china %}

<强> 加成

此代码

for words in dl.find_all('a'):
    links = words.get('href')
    link_text = words.text

    if 'university' in link_text.lower():
        chinaAds.append([links, link_text])

    if 'college' in link_text.lower():
        chinaAds.append([links, link_text])

for ad in chinaAds:
    china.append(tuple(ad))  

都可以优化为:

for words in dl.find_all('a'):
    links = words.get('href')
    link_text = words.text
    link_lower = link_text.lower()

    if ('university' in link_lower) or ('college' in link_lower):
        chinaAds.append((links, link_text))

然后只渲染chinaAds

#fix indentation of your return statement, on your actual code
#it seems belonging to the for loop
return render_template("eslJobs.html", china=chinaAds)