【发布时间】:2017-03-18 15:50:35
【问题描述】:
我正在尝试将元组列表打印到我的 html Flask 模板中,但出现此错误:
self.name, self.filename)
jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'
当我在浏览器中输入localhost 地址时,出现 500 内部服务器错误
如何将我的数据打印到浏览器上?
代码: 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()
代码: 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 & 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>
【问题讨论】:
-
你能发布完整的回溯错误消息吗?我不清楚在哪里将它与你发布的代码相关联。
-
@IronFist 错误信息很长。
标签: python html flask beautifulsoup jinja2