目次

HTMLファイル生成(Python)

 自分のサイトをもっていると、HTMLファイルに
 必要な情報を書き込んで、サーバーに転送する
 作業が発生します。

 HTMLファイルには、タグを入れた文書を書き込んでいきますが
 多くのHTMLファイルをエディタのみで作成すると、タイプミス
 を誘発します。スクリプトで、最低限のタグを入れたファイル
 を作成しています。

 Webサイトでは、index.htmから目的ページに分岐する
 方式をとっています。



 index.htmと目的ページの2種類のHTMLファイルを作成します。
 index.htmには、目的ページへのリンクを記述します。

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=x-sjis">
<meta name="generator" content="Adobe GoLive 4">
<title>Title List</title>
</head>
<body bgcolor="#f0ff99" vlink="#666633">
<h1>
???<br>
</h1>
<font size="2">
<table border="0" cellpadding="0" cellspacing="14" width="285">
<h1>
<tr><td><a href="hoge00.htm"><font size="2">
<nobr>???</nobr></font>
</a></td></tr>
<tr><td><a href="hoge01.htm"><font size="2">
<nobr>???</nobr></font>
</a></td></tr>
<tr><td><a href="hoge02.htm"><font size="2">
<nobr>???</nobr></font>
</a></td></tr>
<tr><td><a href="hoge03.htm"><font size="2">
<nobr>???</nobr></font>
</a></td></tr>
<tr><td><a href="hoge04.htm"><font size="2">
<nobr>???</nobr></font>
</a></td></tr>
<tr><td><a href="hoge05.htm"><font size="2">
<nobr>???</nobr></font>
</a></td></tr>
<tr><td><a href="hoge06.htm"><font size="2">
<nobr>???</nobr></font>
</a></td></tr>
<tr><td><a href="hoge07.htm"><font size="2">
<nobr>???</nobr></font>
</a></td></tr>
<tr><td><a href="hoge08.htm"><font size="2">
<nobr>???</nobr></font>
</a></td></tr>
<tr><td><a href="hoge09.htm"><font size="2">
<nobr>???</nobr></font>
</a></td></tr>
<tr><td><a href="hoge10.htm"><font size="2">
<nobr>???</nobr></font>
</a></td></tr>
</table>
<hr>
<a href="../index.htm">return</a>
</h1>
</body>
</html>

 index.htmファイルを生成する場合、ファイル名といくつの
 ファイルを利用するのかを、情報としてテキストファイルに
 入れておきます。

hoge 11

 テキストファイルのファイル名は、「htbl.txt」と固定。
 固定名なので、スクリプトを使うときに、パラメータを
 指定しなくてもよいです。

 テキストファイルから、ファイル名と個数を取出す処理は
 次のように定義しました。

# open
fin = open('htbl.txt','r')
# get parameters
line = fin.read()
dout = line.split('\n')
# close
fin.close()

# HTML file name and acount
xline = dout[0].split(' ')

print xline[0],xline[1]

 正しく動くのかを調べておきます。



 次のindex.htmファイルを生成する関数を定義します。

def mkIdx(x) :
  # separate
  fname = x[0]
  fcnt  = int(x[1])
  # make index.htm file
  fout = open('index.htm','w')
  # header
  fout.write("<html>\n");
  fout.write("<head>\n");
  fout.write("<meta http-equiv=\"content-type\" content=\"text/html;charset=x-sjis\">\n")
  fout.write("<meta name=\"generator\" content=\"Adobe GoLive 4\">\n")
  fout.write("<title>Title List</title>\n")
  fout.write("</head>\n")
  fout.write("<body bgcolor=\"#f0ff99\" vlink=\"#666633\">\n")
  fout.write("<h1>\n")
  fout.write("???<br>\n")
  fout.write("</h1>\n")
  fout.write("<font size=\"2\">\n")
  fout.write("<table border=\"0\" cellpadding=\"0\" cellspacing=\"14\" width=\"285\">\n")
  fout.write("<h1>\n")
  # record
  for e in range(0,fcnt) :
    # generate 
    xstr = "<tr><td><a href=" + "\"" + fname + str(e)
    if e < 10 :
      xstr = "<tr><td><a href=" + "\"" + fname + "0" + str(e)
    xstr += ".htm\"><font size=\"2\">\n"
    fout.write(xstr)
    fout.write("<nobr>???</nobr></font>\n")
    fout.write("</a></td></tr>\n")
  # footer
  fout.write("</table>\n")
  fout.write("<hr>\n")
  fout.write("<a href=\"../index.htm\">return</a>\n")
  fout.write("</h1>\n")
  fout.write("</body>\n")
  fout.write("</html>\n")
  #
  fout.close()

 ファイル名と個数をリストに入れて渡されたならば
 個数分のファイル名を生成し、index.htmにタグと
 ともに書き出すだけにしました。

 関数がファイルを生成し、その内容が適切かを調べます。
  そのスクリプトの内容は、以下。

#
def mkIdx(x) :
  # separate
  fname = x[0]
  fcnt  = int(x[1])
  # make index.htm file
  fout = open('index.htm','w')
  # header
  fout.write("<html>\n");
  fout.write("<head>\n");
  fout.write("<meta http-equiv=\"content-type\" content=\"text/html;charset=x-sjis\">\n")
  fout.write("<meta name=\"generator\" content=\"Adobe GoLive 4\">\n")
  fout.write("<title>Title List</title>\n")
  fout.write("</head>\n")
  fout.write("<body bgcolor=\"#f0ff99\" vlink=\"#666633\">\n")
  fout.write("<h1>\n")
  fout.write("???<br>\n")
  fout.write("</h1>\n")
  fout.write("<font size=\"2\">\n")
  fout.write("<table border=\"0\" cellpadding=\"0\" cellspacing=\"14\" width=\"285\">\n")
  fout.write("<h1>\n")
  # record
  for e in range(0,fcnt) :
    # generate 
    xstr = "<tr><td><a href=" + "\"" + fname + str(e)
    if e < 10 :
      xstr = "<tr><td><a href=" + "\"" + fname + "0" + str(e)
    xstr += ".htm\"><font size=\"2\">\n"
    fout.write(xstr)
    fout.write("<nobr>???</nobr></font>\n")
    fout.write("</a></td></tr>\n")
  # footer
  fout.write("</table>\n")
  fout.write("<hr>\n")
  fout.write("<a href=\"../index.htm\">return</a>\n")
  fout.write("</h1>\n")
  fout.write("</body>\n")
  fout.write("</html>\n")
  #
  fout.close()

# open
fin = open('htbl.txt','r')
# get parameters
line = fin.read()
dout = line.split('\n')
# close
fin.close()

# HTML file name and acount
xline = dout[0].split(' ')

# generate index.htm
mkIdx(xline)

 ファイルが作成されているかをチェック。



 ファイル内容を確認します。



 個別のHTMLファイルを生成する関数を定義します。

def mkBody(x) :
  # separate
  fname = x[0]
  fcnt  = int(x[1])
  # loop
  for e in range(0,fcnt) :
    # generate file name
    fnamex = fname + str(e) + ".htm"
    if e < 10 :
      fnamex = fname + "0"+ str(e) + ".htm"
    # open
    fout = open(fnamex,'w')
    # record
    fout.write("<html>\n")
    fout.write("<head>\n")
    fout.write("<meta http-equiv=\"content-type\" content=\"text/html;charset=x-sjis\">\n")
    fout.write("<meta name=\"generator\" content=\"Adobe GoLive 4\">\n")
    fout.write("<title>???</title>\n")
    fout.write("</head>\n")
    fout.write("<body bgcolor=\"#ffff99\" vlink=\"#66666\">\n")
    fout.write("<h3>\n")
    fout.write("<a href=\"index.htm\">index</a>\n")
    # judge 
    if e > 0 :
      ee = e-1
      xstr = "<a href=\"" + fname + str(ee)
      if ee < 10 :
        xstr = "<a href=\"" + fname + "0" + str(ee)
      xstr += ".htm\">previous</a>\n"
      fout.write(xstr)
    if e < fcnt-1 :
      ee = e+1
      xstr = "<a href=\"" + fname + str(ee)
      if ee < 10 :
        xstr = "<a href=\"" + fname + "0" + str(ee)
      xstr += ".htm\">next</a>\n"
      fout.write(xstr)
    fout.write("<hr>\n")
    fout.write("<font color=\"#0000ff\">\n")
    fout.write("<h1>???</h1>\n")
    fout.write("</font>\n")
    fout.write("<pre>\n")
    fout.write("(under construction)\n")
    fout.write("</pre>\n")
    fout.write("<hr>\n")
    fout.write("<a href=\"index.htm\">index</a>\n")
    # judge
    if e > 0 :
      ee = e-1
      xstr = "<a href=\"" + fname + str(ee)
      if ee < 10 :
        xstr = "<a href=\"" + fname + "0" + str(ee)
      xstr += ".htm\">previous</a>\n"
      fout.write(xstr)
    if e < fcnt-1 :
      ee = e+1
      xstr = "<a href=\"" + fname + str(ee)
      if ee < 10 :
        xstr = "<a href=\"" + fname + "0" + str(ee)
      xstr += ".htm\">next</a>\n"
      fout.write(xstr)
    fout.write("</h3>\n")
    fout.write("</body>\n")
    fout.write("</html>\n")
  #
  fout.close()

 ファイル名と個数をリストに入れて渡されたならば
 ファイル名の後に、番号を付加しておきます。
 そのファイルに、必要なタグを書き出します。

 まとめると、次のようになります。

#
def mkIdx(x) :
  # separate
  fname = x[0]
  fcnt  = int(x[1])
  # make index.htm file
  fout = open('index.htm','w')
  # header
  fout.write("<html>\n");
  fout.write("<head>\n");
  fout.write("<meta http-equiv=\"content-type\" content=\"text/html;charset=x-sjis\">\n")
  fout.write("<meta name=\"generator\" content=\"Adobe GoLive 4\">\n")
  fout.write("<title>Title List</title>\n")
  fout.write("</head>\n")
  fout.write("<body bgcolor=\"#f0ff99\" vlink=\"#666633\">\n")
  fout.write("<h1>\n")
  fout.write("???<br>\n")
  fout.write("</h1>\n")
  fout.write("<font size=\"2\">\n")
  fout.write("<table border=\"0\" cellpadding=\"0\" cellspacing=\"14\" width=\"285\">\n")
  fout.write("<h1>\n")
  # record
  for e in range(0,fcnt) :
    # generate 
    xstr = "<tr><td><a href=" + "\"" + fname + str(e)
    if e < 10 :
      xstr = "<tr><td><a href=" + "\"" + fname + "0" + str(e)
    xstr += ".htm\"><font size=\"2\">\n"
    fout.write(xstr)
    fout.write("<nobr>???</nobr></font>\n")
    fout.write("</a></td></tr>\n")
  # footer
  fout.write("</table>\n")
  fout.write("<hr>\n")
  fout.write("<a href=\"../index.htm\">return</a>\n")
  fout.write("</h1>\n")
  fout.write("</body>\n")
  fout.write("</html>\n")
  #
  fout.close()

#
def mkBody(x) :
  # separate
  fname = x[0]
  fcnt  = int(x[1])
  # loop
  for e in range(0,fcnt) :
    # generate file name
    fnamex = fname + str(e) + ".htm"
    if e < 10 :
      fnamex = fname + "0"+ str(e) + ".htm"
    # open
    fout = open(fnamex,'w')
    # record
    fout.write("<html>\n")
    fout.write("<head>\n")
    fout.write("<meta http-equiv=\"content-type\" content=\"text/html;charset=x-sjis\">\n")
    fout.write("<meta name=\"generator\" content=\"Adobe GoLive 4\">\n")
    fout.write("<title>???</title>\n")
    fout.write("</head>\n")
    fout.write("<body bgcolor=\"#ffff99\" vlink=\"#66666\">\n")
    fout.write("<h3>\n")
    fout.write("<a href=\"index.htm\">index</a>\n")
    # judge 
    if e > 0 :
      ee = e-1
      xstr = "<a href=\"" + fname + str(ee)
      if ee < 10 :
        xstr = "<a href=\"" + fname + "0" + str(ee)
      xstr += ".htm\">previous</a>\n"
      fout.write(xstr)
    if e < fcnt-1 :
      ee = e+1
      xstr = "<a href=\"" + fname + str(ee)
      if ee < 10 :
        xstr = "<a href=\"" + fname + "0" + str(ee)
      xstr += ".htm\">next</a>\n"
      fout.write(xstr)
    fout.write("<hr>\n")
    fout.write("<font color=\"#0000ff\">\n")
    fout.write("<h1>???</h1>\n")
    fout.write("</font>\n")
    fout.write("<pre>\n")
    fout.write("(under construction)\n")
    fout.write("</pre>\n")
    fout.write("<hr>\n")
    fout.write("<a href=\"index.htm\">index</a>\n")
    # judge
    if e > 0 :
      ee = e-1
      xstr = "<a href=\"" + fname + str(ee)
      if ee < 10 :
        xstr = "<a href=\"" + fname + "0" + str(ee)
      xstr += ".htm\">previous</a>\n"
      fout.write(xstr)
    if e < fcnt-1 :
      ee = e+1
      xstr = "<a href=\"" + fname + str(ee)
      if ee < 10 :
        xstr = "<a href=\"" + fname + "0" + str(ee)
      xstr += ".htm\">next</a>\n"
      fout.write(xstr)
    fout.write("</h3>\n")
    fout.write("</body>\n")
    fout.write("</html>\n")
  #
  fout.close()

# open
fin = open('htbl.txt','r')
# get parameters
line = fin.read()
dout = line.split('\n')
# close
fin.close()

# HTML file name and acount
xline = dout[0].split(' ')

# generate index.htm
mkIdx(xline)

# generate each HMTL file
mkBody(xline)

 正しく動作するのかを調べておきます。

 指定しただけのファイルが生成されているかを見ます。



 HTMLファイルの内容が正しいかをチェック。



 どうやら、正しくできているようです。

 index.htmからリンクでアクセスするHTMLファイルは
 最低限のタグだけしか入っていないので、サイト公開
 するのに、必要な内容をテキストエディタでおぎなう
 ようにします。


目次

inserted by FC2 system