我正在尝试在cafe_form
模板上显示从网站抓取的咖啡馆列表。刮板正常工作,但无法在html模板中显示结果。
我正在尝试使用for循环,但似乎无法弄清楚。
这是.py路由代码:
@app.route('/cafes', methods = ['GET', 'POST'])
def cafe():
cafe_form = CafeForm()
cafes_user_city = CafeForm
if cafe_form.validate_on_submit():
##flash(f'The city you chose is {cafe_form.city.data}, The Suburb {cafe_form.suburb.data}')
url_cafes = f"https://www.broadsheet.com.au/{cafe_form.city.data}/guides/best-cafes-{cafe_form.suburb.data}" #f-string adds user suburb
html_cafes = urlopen(url_cafes)
soup_cafe_list = BeautifulSoup(html_cafes, 'html.parser')
type(soup_cafe_list)
cafe_container = soup_cafe_list.find_all("h2", class_= "venue-title")
cafes =[]
for container in cafe_container:
cafes.append(container.text)
print(cafes)
return render_template('cafeform.html', title= 'Search', cafe_form=cafe_form)
这是html模板。
{% extends "base.html" %}
{% block content %}
<h1>Cafes Search</h1>
<form action="" method="post" novalidate>
{{ cafe_form.hidden_tag() }}
<p>
{{ cafe_form.city.label }}<br>
{{ cafe_form.city(size=32) }} <br>
</p>
<p>
{{ cafe_form.suburb.label }}<br>
{{ cafe_form.suburb(size=32) }} <br>
</p>
<p>
{{ cafe_form.submit() }}
</p>
</form>
<h1>Results</h1>
{% for cafe in cafes %}
<div><p> {{ cafes }} </p></div>
{% endfor %}
{% endblock %}
答案 0 :(得分:1)
似乎您没有将cafes
变量传递给模板。可以通过以下方式解决:
return render_template('cafeform.html', title= 'Search', cafe_form=cafe_form, cafes=cafes)
至于模板,您将显示整个cafes
数组,因此只需将cafes
更改为cafe
即可显示元素:
<h1>Results</h1>
{% for cafe in cafes %}
<div><p> {{ cafe }} </p></div>
{% endfor %}