Python 烧瓶:一页,3个功能-只能获得1个功能打印到网页
我有一个有三个功能的程序。每个函数从不同的网页中提取数据,保存到列表中,然后打印列表 我在终端运行了这个,它运行正常 我想把这些列表打印到一个网页上 我可以打印一个函数,但我不知道如何将多个函数放入烧瓶中 以下是有效的方法: .py文件 这是我在.html文件中打印的方式Python 烧瓶:一页,3个功能-只能获得1个功能打印到网页,python,flask,Python,Flask,我有一个有三个功能的程序。每个函数从不同的网页中提取数据,保存到列表中,然后打印列表 我在终端运行了这个,它运行正常 我想把这些列表打印到一个网页上 我可以打印一个函数,但我不知道如何将多个函数放入烧瓶中 以下是有效的方法: .py文件 这是我在.html文件中打印的方式 我将在我的.html页面中的两个新列表中打印这些函数的数据。这里有一个简单的示例,一种方法是简化scraper函数,使其只返回列表,然后将参数添加到render\u template(): index.html <ul
我将在我的.html页面中的两个新列表中打印这些函数的数据。这里有一个简单的示例,一种方法是简化scraper函数,使其只返回列表,然后将参数添加到
render\u template()
:
index.html
<ul class="list-group col-md-4" style="padding-left: 18px;">
{% for href, caption in china %}
<li class="list-group-item"><a href="{{ href }}">{{ caption }}</a></li>
{% endfor %}
</ul>
<ul class="list-group col-md-4" style="padding-left: 18px;">
{% for href, caption in korea %}
<li class="list-group-item"><a href="{{ href }}">{{ caption }}</a></li>
{% endfor %}
</ul>
<ul class="list-group col-md-4" style="padding-left: 18px;">
{% for href, caption in jobs %}
<li class="list-group-item"><a href="{{ href }}">{{ caption }}</a></li>
{% endfor %}
</ul>
{%for href,中文字幕%}
{%endfor%}
{%endfor%}
{%endfor%}
谢谢。我是根据你的建议做的。返回值,然后将函数放入变量中,并在render_模板中调用它们。干杯
<ul class="list-group col-md-4" style="padding-left: 18px;">
{% for href, caption in china %}
<li class="list-group-item"><a href="{{ href }}">{{ caption }}</a></li>
{% endfor %}
</ul>
def koreaJobs():
sauce = urllib.request.urlopen('http://www.eslcafe.com/jobs/korea/').read()
soup = bs.BeautifulSoup(sauce, 'html.parser')
dl = soup.dl
koreaAds = []
korea []
for words in dl.find_all('a'):
links = words.get('href')
link_text = words.text
if ('university' in link_text.lower()) or ('universities' in link_text.lower()) or ('college' in link_text.lower()) or ('colleges' in link_text.lower()):
koreaAds.append([links, link_text])
for ad in koreaAds:
korea.append(tuple(ad))
return render_template("eslJobs.html", korea=korea)
def intlJobs():
sauce = urllib.request.urlopen('http://www.eslcafe.com/joblist/').read()
soup = bs.BeautifulSoup(sauce, 'html.parser')
dl = soup.dl
intlAds = []
intl = []
for words in dl.find_all('a'):
links = words.get('href')
link_text = words.text
if ('university' in link_text.lower()) or ('universities' in link_text.lower()) or ('college' in link_text.lower()) or ('colleges' in link_text.lower()):
intlAds.append([links, link_text])
for ad in intlAds:
intl.append(tuple(ad))
return render_template("eslJobs.html", intl=intl)
from flask import Flask, render_template
import bs4 as bs
import urllib.request
app = Flask(__name__)
def get_ads(url):
""" This function returns a list of tuples. """
terms = ['universit', 'college']
sauce = urllib.request.urlopen(url).read() #
soup = bs.BeautifulSoup(sauce, 'html.parser')
ads = []
for words in soup.dl.find_all('a'):
links = words.get('href')
link_text = words.text
if any(x in link_text.lower() for x in terms):
ads.append(tuple([links, link_text]))
return ads
# call functions here when the app starts
china = get_ads('http://www.example.com/jobs/china/')
korea = get_ads('http://www.example.com/jobs/korea/')
jobs = get_ads('http://www.example.com/joblist/')
@app.route('/')
def index():
# or call functions here each time a page is requested
return render_template("index.html", china=china, korea=korea, jobs=jobs)
if __name__ == '__main__':
app.run(debug=True)
<ul class="list-group col-md-4" style="padding-left: 18px;">
{% for href, caption in china %}
<li class="list-group-item"><a href="{{ href }}">{{ caption }}</a></li>
{% endfor %}
</ul>
<ul class="list-group col-md-4" style="padding-left: 18px;">
{% for href, caption in korea %}
<li class="list-group-item"><a href="{{ href }}">{{ caption }}</a></li>
{% endfor %}
</ul>
<ul class="list-group col-md-4" style="padding-left: 18px;">
{% for href, caption in jobs %}
<li class="list-group-item"><a href="{{ href }}">{{ caption }}</a></li>
{% endfor %}
</ul>