0

Hello I am writing this simple program to get the current value of the USD. I am using two methods, one is a simple API call and the other one is web scraping. Now, I would like to show this information on a html file. any idea on how to? here are 2 of the python files i am working with: this one uses API call:

import requests
import json

# Dolar today

dtUrl = 'https://s3.amazonaws.com/dolartoday/data.json'

def dolartoday():
    response = requests.get(dtUrl)
    format = response.json()["USD"]["transferencia"]
    print("DolarToday:", format)

dolartoday()

And this one uses web scraping:

import requests
from bs4 import BeautifulSoup

# airtm

airtmUrl = 'https://rates.airtm.com/'

def airtm():
    req = requests.get(airtmUrl)
    soup = BeautifulSoup(req.content, 'html.parser')
    rates = soup.findAll('div', class_="text-6xl")
    for rate in rates:
        rate.find('span', class_='rate--general')

    print("AirTM:", rate.text)

airtm()

here is the HTML file (is empty):

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    
  </body>
</html>

I am using python 3 and HTML 5

user14102083
  • 39
  • 1
  • 5
  • What's the exact issue here? You can simply use Python's string formatting to insert any text into the HTML. BeautifulSoup should also be able to modify HTML. – ForceBru Oct 13 '20 at 16:00
  • Instead of `print(...)`, [`return`](https://stackoverflow.com/questions/7129285/what-is-the-purpose-of-the-return-statement) both values then write them out to your HTML file. –  Oct 13 '20 at 16:04
  • @JustinEzequiel alright so i did return rate.text and how would I insert that into HTML file? is there a synatx to do that? – user14102083 Oct 13 '20 at 16:08
  • `template = '

    DolarToday: {}

    AirTM: {}

    '; a = '42.00'; b = '36.00'; print(template.format(a, b))` -- see https://docs.python.org/3/library/string.html#string.Formatter.format
    –  Oct 13 '20 at 16:53

1 Answers1

0

you can certainly write the information into a HTML file but the best thing you can do is to use some framework, either Django or Flask.

htmlfile = file.open('application.html', 'w')
htmlfile.write(parsedhtml)
diego
  • 409
  • 4
  • 12