SAX将dd.xml解析成html。当然啦,如果得到了xml对应的xsl文件可以直接用libxml2将其转换成html。
复制代码 代码如下:
from xml.sax.handler import ContentHandler
from xml.sax import parse
class Dispatcher:
def dispatch(self, prefix, name, attrs=None):
mname = prefix + name.capitalize()
dname = 'default' + prefix.capitalize()
method = getattr(self, mname, None)
if callable(method): args = ()
else:
method = getattr(self, dname, None)
#if prefix == 'start': args += attrs
if callable(method): method()
def startElement(self, name, attrs):
self.dispatch('start', name, attrs)
def endElement(self, name):
self.dispatch('end', name)
class Website(Dispatcher, ContentHandler):
def __init__(self):
self.fout = open('ddt_SAX.html', 'w')
self.imagein = False
self.desflag = False
self.item = False
self.title = ''
self.link = ''
self.guid = ''
self.url = ''
self.pubdate = ''
self.description = ''
self.temp = ''
self.prx = ''
def startChannel(self):
self.fout.write('''<html>\n<head>\n<title> RSS-''')
def endChannel(self):
self.fout.write('''
<tr><td height="20"></td></tr>
</table>
</center>
<script>
function GetTimeDiff(str)
{
if(str == '')
{
return '';
}
var pubDate = new Date(str);
var nowDate = new Date();
var diffMilSeconds = nowDate.valueOf()-pubDate.valueOf();
var days = diffMilSeconds/86400000;
days = parseInt(days);
diffMilSeconds = diffMilSeconds-(days*86400000);
var hours = diffMilSeconds/3600000;
hours = parseInt(hours);
diffMilSeconds = diffMilSeconds-(hours*3600000);
var minutes = diffMilSeconds/60000;
minutes = parseInt(minutes);
diffMilSeconds = diffMilSeconds-(minutes*60000);
var seconds = diffMilSeconds/1000;
seconds = parseInt(seconds);
var returnStr = "±±¾(C)・¢²¼Ê±¼a£º" + pubDate.toLocaleString();
if(days > 0)
{
returnStr = returnStr + " £¨¾aÀeÏÖÔÚ" + days + "Ìi" + hours + "Сʱ" + minutes + "・ÖÖÓ£(C)";
}
else if (hours > 0)
{
returnStr = returnStr + " £¨¾aÀeÏÖÔÚ" + hours + "Сʱ" + minutes + "・ÖÖÓ£(C)";
}
else if (minutes > 0)
{
returnStr = returnStr + " £¨¾aÀeÏÖÔÚ" + minutes + "・ÖÖÓ£(C)";
}
return returnStr;
}
function GetSpanText()
{
var pubDate;
var pubDateArray;
var spanArray = document.getElementsByTagName("span");
for(var i = 0; i < spanArray.length; i++)
{
pubDate = spanArray[i].innerHTML;
document.getElementsByTagName("span")[i].innerHTML = GetTimeDiff(pubDate);
}
}
GetSpanText();