web.py等框架如何生成静态网页
为啥需要生成静态网页,静态网页方便搜素引擎的收录,还可以减少访问数据库的压力。减少带宽的占用。
web.py这么干,django估计也可以这么干,估计还有其它的好方法,本人借鉴以前php生成的经验。
将以前的思路移植到web.py上来。
在web.py安装的路径下有一个static目录,创建一个model.html的文件
文件内容如下。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{dwmc} 七七巴巴黄页网</title>
<!-- Meta Data -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="{dwmc}电话 | {dwmc}地址" />
<meta name="description" content="{dwmc}简介:{yyfw2}....." />
<!-- Site Title -->
<!-- Link to Style External Sheet -->
<link href="../../css/style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="page_wrapper">
<div id="header_wrapper">
<div id="header">
<h1>七七巴巴黄页网-专业黄页网站</font></h1>
</div>
<div id="navcontainer">
<ul id="navlist">
<li id="active"><a href="http://www.qy7788.com.cn/index.html" id="current">分省黄页</a></li>
<li><a href="http://www.qy7788.com.cn/hy.html">行业黄页</a></li>
<li><a href="http://www.qy7788.com.cn/hykx.html">行业快讯</a></li>
<li><a href="http://www.qy7788.com.cn/mfcx.html">免费查询</a></li>
<li><a href="http://www.qy7788.com.cn/xclqy.html">新成立企业</a></li>
<li><a href="http://www.qy7788.com.cn/guanyu.html">关于</a></li></ul>
</div>
</div>
<div id="left_side">
</div>
<div id="right_side">
</div>
<div id="content">
<div class='featurebox_center'>
<script language="JavaScript" src="../../bd.js" type="text/javascript"></script><BR>
公司名称:{dwmc} <BR />
公司地址:{dz} <BR />
{dwmc}电话:{tel}<BR />
传真:{fax}<BR />
公司网站:{www}<BR />
联系人:{lxr}<BR />
企业性质:{xz}<BR />
企业规模:{gm}<BR />
公司简介:{yyfw}<BR />
<script language="JavaScript" src="../../bd.js" type="text/javascript"></script><BR>
其它相关企业:<BR />
{link}
</div>
<p>
</p>
</div>
<div id="footer">
七七巴巴黄页网所有
</div>
</div>
</body>
</html>
下面是生成每条记录的静态网页实例。
# -*- coding: UTF-8 -*-
import MySQLdb as mdb
import sys
#获得mysql查询的链接对象
con = mdb.connect(host='localhost',user= '****', passwd='*****', db='*****',charset='utf8')
#获取连接上的字典cursor,注意获取的方法,
#每一个cursor其实都是cursor的子类
cur = con.cursor(mdb.cursors.DictCursor)
cur.execute("SELECT * FROM ml where DWMC limit 0,10 ")
#获取数据方法不变
rows = cur.fetchall()
#print rows
#遍历数据也不变(比上一个更直接一点)
for row in rows:
print row["DWMC"],row["DZ"]
infile = open("model.html", "r")
outline = infile.read()
outfile = open("shanghai_"+str(row["ID"])+".html","w")
outline = outline.replace('{dwmc}', row["DWMC"].encode('utf8'))
outline = outline.replace('{dz}', row["DZ"].encode('utf8'))
outfile.write(outline)
outfile.close()
infile.close()
转载请注明来源:http://www.qy7788.com.cn/shiyongxinxi/shiyongxinxi129.html
代码很简单吧,主要是思路。