Solution 1 :

I’d suggest making a separate module which does the scraping:

scrape.py

from urllib.request import urlopen as uReq

from bs4 import BeautifulSoup as soup
from bs4 import NavigableString

def get_deaths():
    page_url = 'https://www.cdc.gov/coronavirus/2019-ncov/cases-updates/cases-in-us.html'
    print ('Hitting URL: ', page_url)
    uClient = uReq(page_url)
    page_soup = soup(uClient.read(), "html.parser")
    uClient.close()
    containers = page_soup.findAll("div",{"class":"callout"})
    Deaths = containers[0].span.text

    return Deaths

if __name__ == '__main__':
    deaths = get_deaths()
    print (deaths)

You can now run this manually with python scrape.py or import the function to another module which is your flask app. Of course this is only pulls a single figure. But if you did want to render this in a table you could do:

app.py

from flask import Flask, render_template
from scrape import get_deaths

app = Flask(__name__)

@app.route('/')
def index():
    # Create a list of column headers
    headers = ['Deaths']

    # Now the data, a list where each item is a list containing the row values
    objects = [[ get_deaths() ]]

    return render_template('index.html', headers=headers, objects=objects)

Which can then be rendered with the following template:

templates/index.html

{% if objects %}
  <table id='result' class='display'>
    <thead>
      <tr>
      {% for header in headers %}
        <th>{{header}}</th>
      {% endfor %}
      </tr>
    </thead>

    <tbody>
    {% for object in objects %} 
      <tr>
    {% for item in object %}
        <td>{{item}}</td>
      {% endfor %}
      </tr>
    {% endfor %}        
    </tbody>
  </table>
{% endif %}

Notice that this avoids hard coding column names into the template file, provided you pass the headers list yourself from Python.

Run this with flask run and connect to http://localhost:5000 in your browser.

You may also wish to have a look at my repo search-generic-tables which includes the JS library Datatables in the template to add things like search and pagination on the fly.


Be aware that the above code re-scrapes the site every time someone hits the page. There are a few ways around this. One option is to use flask_caching so run pip install flask_caching and then decorate the index view function to define a timeout.

# ...

app = Flask(__name__)

from flask_caching import Cache
cache = Cache(config={'CACHE_TYPE': 'simple'})
cache.init_app(app)

@app.route('/')
@cache.cached(timeout=50)
def index():
    # Rest of code.

Now the API result will be cached for 50 seconds.

Problem :

I am trying to make a web-scraper that will get data from corona-virus websites. I have made the scraper I was just curious as to how to pass that data I get to my html code. I know this type of website has been done before I just want to do it for my own personal enjoyment. Here is my python code.

from urllib.request import urlopen as uReq

from bs4 import BeautifulSoup as soup

import csv

from bs4 import NavigableString

from flask import Flask


page_url = 'https://www.cdc.gov/coronavirus/2019-ncov/cases-updates/cases-in-us.html

uClient = uReq(page_url)

page_soup = soup(uClient.read(), "html.parser")

uClient.close()

containers = page_soup.findAll("div",{"class":"callout"})

out_filename = "usacovid_deaths.csv"

headers = "Deaths n"

Deaths = containers[0].span.text

print(Deaths +"n")

with open(out_filename, 'w', newline='') as csvfile:
    spamwriter = csv.writer(csvfile)
    spamwriter.writerow([Deaths])

html_file_smth = 'C:\Users\tshor\OneDrive\Desktop\HTML\Stats.html'

html_file_smth2 = 'C:\Users\tshor\OneDrive\Desktop\HTML\Stats2.html'

with open(html_file_smth, 'r', encoding = "utf-8") as htmll:

    reader = htmll.read()

html_content = soup(reader, 'html.parser')

tr_tag = html_content.find(attrs = {"id":"use-id"})

tr_tag.insert(0, NavigableString(str(Deaths)))

with open(html_file_smth2, 'w',encoding="utf-8") as html2:

    html2.write(reader)

It says flask as an import but I dont think that is something that I need. Also, I know it is broad question but I am very lost so any help would be great. Thanks.

Comments

Comment posted by tshore

oh wow. Thank you so much. I am just confused as to where to put this in my code. Is there any way I could email you. Im sorry I am very much so a beginner @v25.

Comment posted by v25

@tshore please feel free to log an issue against that repo if you wish

Comment posted by tshore

where do I put this in my html code. I have a table for each country so where do I put this in a table.

By