nasan nasan -4 years ago 109
Python Question

Flask: one page, 3 functions - can only get 1 function to print to webpage

I have a program with three functions. Each function scrapes data from a different webpage, saves it to a list, and prints the list.

I ran this in Terminal, and it works.

I want to print these lists onto a single webpage.

I can get one function to print, but I don't know how to put multiple functions into flask.

Heres what works:


.py file


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)



This is how I print in my .html file


<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>



Stuff I want to add to .py file


The two functions I want to add are the same as the first just with different variables and they scrape from different webpages

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)


I will print the data from these functions in two new lists in my .html page

Answer Source

Here's a quick example, one way would be to simplify the scraper function(s) so they just return lists, then add args to 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>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download