我是python的新手,正在学习如何将列表从python传递到模板。 因此,从下面的代码中, “结果”是我从字幕数据中提取的主题标签的列表。
我想做的是将此列表传递给模板,并使用jinja进行循环以在html文件中显示。(主要是我不确定我的displayHashtag()函数是否正确)
import requests
import re
from flask import Flask,
render_template
app = Flask(__name__)
TARGETURL = "https://www.instagram.com/"
USERNAME = "username"
r = requests.get( TARGETURL + USERNAME + "/")
def getCaption(text):
captions =
re.findall(r"edge_media_to_caption\":{\"edges\":\[{\"node\":{\"text\":\"(.+?)\"}",text)
return captions
def getHashtags(text):
hashtags = re.findall(r"#([a-zA-Z]+)", text)
return hashtags
printCaption = getCaption(r.text)
printHashtags =
getHashtags(printCaption[0])
result = []
for caption in printCaption :
result += getHashtags(caption)
print("Show me all the hashtags : ")
print(result)
@app.route('/')
def displayHashtag():
return render_template('HashtagList.html', result = result)
if __name__ == '__main__':
app.run(debug=True)
HashtagList文件----
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<ul>
{% for hashtag in result %}
<li>{{ hashtag }}</li>
{% endfor %}
</ul>
谢谢!