我有一个包含三个功能的程序。每个函数从不同的网页上抓取数据,将其保存到列表中,然后打印列表。
我在终端运行它,它可以工作。
我想将这些列表打印到一个网页上。
我可以打印一个功能,但我不知道如何将多个功能放入烧瓶中。
继承人的作品:
.py文件
from flask import Flask, render_template
import bs4 as bs
import urllib.request
app = Flask(__name__)
@app.route('/')
def chinaAds():
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()) or ('universities' in link_text.lower()) or ('college' in link_text.lower()) or ('colleges' in link_text.lower()):
chinaAds.append([links, link_text])
for ad in chinaAds:
china.append(tuple(ad))
return render_template("eslJobs.html", china=china)
这是我在.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>
我要添加到.py文件
的东西
我要添加的两个函数与第一个只有不同变量的函数相同,它们从不同的网页中删除
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)
我将在我的.html页面中的两个新列表中打印这些函数中的数据
答案 0 :(得分:1)
这是一个简单的例子,一种方法是简化刮刀功能,使它们只返回列表,然后将参数添加到render_template()
:
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)
的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>