import time from xml.dom.minidom import parse html = """ <!DOCTYPE html> <html> <head> <title>Python创意编程_李兴球博客目录2019-8-22版</title> <meta content="text/html; charset=utf-8" http-equiv="content-type" /> <link rel="stylesheet" type="text/css" href="link.css"></link> </head> <body>\n """ d = f"<h1>李兴球Python创意编程目录</h1>\n本网页由python程序于 {time.ctime()} 读取wordpress博客自动生成。" html = f"{html}\n{d}\n<ol style='line-height:35px'>" li_list = [html] DOMTree = parse('_python.wordpress.2019-08-22 (1).xml') item = DOMTree.documentElement titles = item.getElementsByTagName('title') links = item.getElementsByTagName('link') for 标题,链接 in zip(titles[::-1],links[::-1]): for title,link in zip(标题.childNodes,链接.childNodes): t = title.nodeValue u = link.nodeValue if isinstance(t,str) and isinstance(u,str): s = f"<li><a target=_blank href='{u}' > {t} </a></li>\n" li_list.append(s) ad = "<li><a target=_blank href='http://www.scratch8.net'>风火轮编程Python课程体系简介。</a></li>\n" li_list.append(ad) li_list.append("\n</ol></body></html>") print('共有',len(li_list),"项数据") f = open("Python创意编程目录.html",mode='w',encoding='utf-8') f.write("".join(li_list)) f.close()