如何在python中链接HTML文件

如何在python中链接HTML文件,python,Python,我有: 我正在为每个水果和大小组合创建具有水果属性的HTML页面 基本上,我的python脚本创建的文件夹名为: fruits = [apple, banana, pineapple, oranges] sizes = [small, medium, large] 每个水果文件夹都有三个子文件夹,名称为: apple, banana, pineapple, oranges 每个文件夹都包含一个具有各自大小名称的HTML文件。像 small, medium, large 并且它包含特定水果

我有:

我正在为每个水果和大小组合创建具有水果属性的HTML页面

基本上,我的python脚本创建的文件夹名为:

fruits = [apple, banana, pineapple, oranges] 
sizes = [small, medium, large]
每个水果文件夹都有三个子文件夹,名称为:

apple, banana, pineapple, oranges
每个文件夹都包含一个具有各自大小名称的HTML文件。像

small, medium, large
并且它包含特定
水果大小组合的
水果属性

因此,所有html页面都具有类似的路径,如下所示:

small has small.html 
medium has medium.html
large has large.html 
现在我想将所有这些页面链接到我的HTML索引页面。但是我是python新手,不知道如何使用path(我可以使用
os.path.join创建path)
在python中使用href

以下是我的代码:

script-path/fruit/size/size.html 
def html_home(水果,大小):
htmlFile=open(水果+“.html”,“w”)
htmlFile.write(“!DOCTYPE html>\n”)
htmlFile.write(“\n”)
htmlFile.write(“水果属性\n”)
htmlFile.write(“”)
htmlFile.write(“”)
htmlFile.write(“水果属性列表\n”)
#当我调用html_home()函数时,已经有了
#已在每个水果/大小文件夹中创建相应的size.html页面。
#它们的路径都是:scriptpath/fruit/size/size.html
scriptpath=os.path.dirname(os.path.abspath(_文件__))
htmlpath=os.path.join(脚本路径、目标、大小、大小+“.html”)
htmlFile.write(“”
“”) htmlFile.write(“\n”) htmlFile.write(“\n”) htmlFile.close() #下面的代码可以正常工作 htmlFile=open('home.html','w') htmlFile.write(“\n”) htmlFile.write(“\n”) htmlFile.write(“\n”) htmlFile.write(“水果属性\n”) htmlFile.write(“\n”) 对于水果中的水果: htmlFile.write(“
\n”) htmlFile.write(“\n”)
htmlFile.write(“您的问题可能是由于在

def html_home(fruit,size):
    htmlFile = open(fruit+".html","w")
    htmlFile.write("!DOCTYPE html>\n")
    htmlFile.write("<html>\n")
    htmlFile.write("<title> Fruitproperites </title>\n")
    htmlFile.write("<head>")
    htmlFile.write("<body>")
    htmlFile.write("<h1> List of fruitproperties </h1>\n")
    # By the time, I call html_home() function, there is already     
    # respective size.html page been created in each fruit/size folder.
    #  all have path as: scriptpath/fruit/size/size.html 
    scriptpath = os.path.dirname(os.path.abspath(__file__))
    htmlpath = os.path.join(scriptpath,target,size,size+".html")
    htmlFile.write('<a href = htmlpath> +size+ '</a>'<br><\n>')  
    htmlFile.write("</body>\n")
    htmlFile.write("</html>\n")
    htmlFile.close() 

    # Following code works fine 
    htmlFile = open('home.html','w')
    htmlFile.write("<!DOCTYPE html> \n")
    htmlFile.write("<html>\n")
    htmlFile.write("<head>\n")
    htmlFile.write("<title> Fruitproperties</title>\n")
    htmlFile.write("<body>\n")
    for fruit in fruits:
        htmlFile.write('<a href="'+fruit+'.html"> List of functions -'+target+'</a><br>\n')
    htmlFile.write("<body>\n")
    htmlFile.write("</html?\n")
    htmlFile.close() 
htmlFile.write(“”
“)
它还会创建糟糕的HTML

您应该使用另一种编写HTML的方法,即使用模板字符串的简单方法。Python中的字符串可以跨越多行,然后您可以创建上下文词典并将其用于字符串格式:

htmlFile.write('<a href = htmlpath> +size+ '</a>'<br><\n>')
templ=''
果业
物业一览表

''' 上下文={} 上下文['href']='http://yoursite.tld/' 上下文['size']=size html=templ%上下文
然后立即编写html内容,就像您已经做的那样

要链接到本地文件而不是http资源,请使用
文件:
URI架构。获取此类链接的最简单方法是在浏览器中打开文件并检查URL栏

templ = '''<!DOCTYPE html>
<html>
<title> Fruitproperites </title>
<head>
<body>
<h1> List of fruitproperties </h1>
<a href="%(href)s">%(size)s</a><br>
</body>
</html>'''
context = {}
context['href'] = 'http://yoursite.tld/'
context['size'] = size
html = templ % context