1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| import xml.dom.minidom import os import time import re
class Moment: author = "" homepage = "" title = "" postUrl = "" publishTime = "" postSummary = ""
def __init__(self, author, homepage, title, postUrl, publishTime, postSummary): self.author = author self.homepage = homepage self.title = title self.postUrl = postUrl self.publishTime = publishTime self.postSummary = postSummary
class Tool: urls = []
moments = []
def __init__(self, urls, moments): self.urls = urls self.moments = moments
def parseInfo(self): for url in self.urls:
os.system("curl " + url + " > tmp.xml")
dom = xml.dom.minidom.parse("tmp.xml")
root = dom.documentElement
authorNodes = root.getElementsByTagName("author") author = authorNodes[0].getElementsByTagName("name")[0].firstChild.data
print("作者: ", author)
linkNodes = root.getElementsByTagName("link") homepage = linkNodes[1].getAttribute("href")
print("主页: ", homepage)
postNodes = root.getElementsByTagName("entry") if (postNodes is None): print("一篇文章也没有")
for postNode in postNodes:
print("========")
title = postNode.getElementsByTagName("title")[0].firstChild.data print("文章标题: ", title)
postLinkNode = postNode.getElementsByTagName("link")[0] postUrl = postLinkNode.getAttribute("href") print("文章链接: ", postUrl)
publishTimeStr = postNode.getElementsByTagName("published")[0].firstChild.data publishTime = publishTimeStr[0:10] + " " + publishTimeStr[11:19] print("发布时间", publishTime)
postSummary = "暂无文章摘要" if (len(postNode.getElementsByTagName("summary")) > 0): postSummary = postNode.getElementsByTagName("summary")[0].firstChild.data postSummary = re.sub(r'<.*?>', '', postSummary) postSummary = re.sub(r'<\r\n>', '', postSummary) postSummary = re.sub(r'[\n\"\\]*?', '', postSummary) if len(postSummary) > 200: postSummary = postSummary[0:150] postSummary = postSummary + "..." print("文章摘要: ", postSummary)
moment = Moment(author, homepage, title, postUrl, publishTime, postSummary)
self.moments.append(moment)
def writeContent(self, fullFilePath: str):
jsonStrs = []
sortedMoments = sorted(self.moments, key=lambda Moment: time.strptime(Moment.publishTime, "%Y-%m-%d %H:%M:%S"))
sortedMoments.reverse()
if (len(sortedMoments) > 100): sortedMoments = sortedMoments[0:100]
for moment in sortedMoments: json = (" {\n" " \"author\": \"" + moment.author + "\",\n" " \"homepage\": \"" + moment.homepage + "\",\n" " \"title\": \"" + moment.title + "\",\n" " \"momentUrl\": \"" + moment.postUrl + "\",\n" " \"publishTime\": \"" + moment.publishTime +"\", \n" " \"summary\": \"" + moment.postSummary + "\"\n" " }")
jsonStrs.append(json)
momentContent = "[\n" + ',\n'.join(jsonStrs) + "\n]\n"
print("准备写入文件\n")
with open(fullFilePath, "w") as f: f.write(momentContent) f.close()
print("写入文件完成\n")
if __name__ == '__main__': urls = ["https://www.xiaoliu.life/atom.xml", "https://blog.bxzdyg.cn/atom.xml", "https://blog.liukuan.cc/atom.xml", "https://www.xwsclub.top/atom.xml", "https://b.leonus.cn/atom.xml", "https://liheyuting.github.io/atom.xml", "https://aciano.top/atom.xml", "https://z.arlmy.me/atom.xml", "https://blog.starsharbor.com/atom.xml"] moments = [] tool = Tool(urls, moments) tool.parseInfo() tool.writeContent("source/_data/moments.json") print("主函数处理完成\n")
|