随机链接生成
2020-10-24 python 1520
import random import re import time class LinkTool: def createLink(self, tpl, limit): links = [] for i in range(limit): link = tpl r = re.search("{.+}",link) while r is not None: link = self.replaceTpl(link) r = re.search("{.+}", link) links.append(link) return links def replaceTpl(self, tpl): date = self.date() link = tpl num_len = self.parseTpl("数字", tpl) zimu_len = self.parseTpl("字母", tpl) hunhe_len = self.parseTpl("混合", tpl) if num_len > 0: link = link.replace("{{数字{0}}}".format(num_len), self.randNums(num_len)) if zimu_len > 0: link = link.replace("{{字母{0}}}".format(zimu_len), self.randStrs(zimu_len)) if hunhe_len > 0: link = link.replace("{{混合{0}}}".format(hunhe_len), self.randNumStrs(hunhe_len)) return link.replace("{日期}", date) def parseTpl(self, s, tpl): try: num_m = re.search("{" + s + "(\d)}", tpl) num = int(num_m.groups()[0]) except Exception as e: # print("Error:%s" % str(e)) return 0 return num def randStrs(self, len): tmp = [] for i in range(len): tmp.append(self.randStr()) return "".join(tmp) def randNumStrs(self, len): tmp = [] for i in range(len): tmp.append(self.randNumStr()) return "".join(tmp) def randNumStr(self): sn = "0123456789abcdefghijklmnopqrstuvwxyz" return sn[random.randint(0, 35)] def randNums(self, len): tmp = [] for i in range(len): tmp.append(self.randNum()) return "".join(tmp) def randStr(self): s = "abcdefghijklmnopqrstuvwxyz" return s[random.randint(0, 25)] def randNum(self): n = "0123456789" return n[random.randint(0, 9)] def randNumStr(self): sn = "0123456789abcdefghijklmnopqrstuvwxyz" return sn[random.randint(0, 35)] def date(self): return time.strftime("%Y%M%d", time.localtime())
很赞哦! (0)
相关文章
文章评论
-
-
-
0条评论