I'm working on a project for school and was just a bit stumped with this. I need to insert the text value from the python variable title1
into an h3
tag. This needs to be done onclick
of a button. However, I'm unsure as to how to do this as the code to create the HTML page is all in triple quotes which prevent inserting variables. Any help on how to do this would be appreciated.
the code is shown below:
student_number = 11281499 # put your student number here as an integer
student_name = 'Jacob Garofalo'
from sys import exit as abort
from urllib.request import urlopen
from tkinter import *
from tkinter.scrolledtext import ScrolledText
from tkinter.ttk import Progressbar
def download(url = 'http://www.wikipedia.org/',
target_filename = 'downloaded_document',
filename_extension = 'html',
save_file = True,
char_set = 'UTF-8',
incognito = False):
# Import the function for opening online documents and
# the class for creating requests
from urllib.request import urlopen, Request
# Import an exception raised when a web server denies access
# to a document
from urllib.error import HTTPError
# Import an exception raised when a web document cannot
# be downloaded
from urllib.error import URLError
# Open the web document for reading
try:
if incognito:
# Pretend to be a web browser instead of
# a Python script (NOT RELIABLE OR RECOMMENDED!)
request = Request(url)
request.add_header('User-Agent',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/42.0.2311.135 Safari/537.36 Edge/12.246')
print("Warning - Request does not reveal client's true identity.")
print(" This is both unreliable and unethical!")
print(" Proceed at your own risk!\n")
else:
# Behave ethically
request = url
web_page = urlopen(request)
except ValueError:
print("Download error - Cannot find document at URL '" url "'\n")
return None
except HTTPError:
print("Download error - Access denied to document at URL '" url "'\n")
return None
except URLError:
print("Download error - Cannot access URL '" url "'\n")
return None
except Exception as message:
print("Download error - Something went wrong when trying to download " \
"the document at URL '" url "'")
print("Error message was:", message, "\n")
return None
# Read the contents as a character string
try:
web_page_contents = web_page.read().decode(char_set)
except UnicodeDecodeError:
print("Download error - Unable to decode document from URL '" \
url "' as '" char_set "' characters\n")
return None
except Exception as message:
print("Download error - Something went wrong when trying to decode " \
"the document from URL '" url "'")
print("Error message was:", message, "\n")
return None
# Optionally write the contents to a local text file
# (overwriting the file if it already exists!)
if save_file:
try:
text_file = open(target_filename '.' filename_extension,
'w', encoding = char_set)
text_file.write(web_page_contents)
text_file.close()
except Exception as message:
print("Download error - Unable to write to file '" \
target_filename "'")
print("Error message was:", message, "\n")
# Return the downloaded document to the caller
return web_page_contents
global text
text = '''
<!DOCTYPE html>
<html>
<head>
<title>Hirams live entertainment</title>
</head>
<style>
body {
background-color: white;
}
</style>
</div>
<h1>Admit One</h1>
<h2>This is your ticket courtesy of Hiram's live entertainment</h2>
<h3></h3>
<img src="https://www.nicepng.com/png/full/292-2923561_live-entertainment-lives-here-live-entertainment-png.png" width="700" height="1200" id="live">
</body>
</html>
'''
def tivoli():
tivoli = download("https://cinebar.com.au/movies/coming-soon/")
f = urllib.request.urlopen("https://cinebar.com.au/movies/coming-soon/")
page = f.read().decode("utf-8")
#code to find the title
tivoli_title = page.find('<h4 >')
start_title = tivoli_title len('<h4 >')
end_title = page.find("</h4>")
global title1
title1 = tivoli[start_title:end_title]
#code to find the date
tivoli_title1 = page.find('<small>')
start_title1 = tivoli_title1 len('<small>')
end_title1 = page.find("</small>")
global title4
title4 = tivoli[start_title1:end_title1]
f.close()
def print_ticket():
if t.get() == 1:
webbrowser.open(cinb)
print_ticket = Button(frame_options, text="print ticket", command=print_ticket)
CodePudding user response:
I guess this will do it
text = text.replace('<h3></h3>', f'<h3>{title1}</h3>', 1)