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
| import re import requests import random import linecache import os
def gethtml(): url = 'https://htmfiles.englishhome.org/Japsurnames/Japsurnames-' filename = 'result.txt' i = 0 r = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k1', 'k2', 'm', 'n', 'o', 'r', 's', 't', 'u', 'w', 'v', 'z']
f = open(filename, 'a+', encoding='utf-8') for i in range(22): htr = url + str(r[i]) + str('.htm') response = requests.get(htr) response.encoding = response.apparent_encoding html = response.text pattern = re.compile('<td width="45%"(.+)</td>') result = pattern.findall(html,re.S) for line in result: f.write(line + '\n') f.close()
def correct(): i = 0 subp = ['。', '>', ' height="[0-9][0-9]"'] filename = 'result.txt' for i in range(3): f = open(filename, 'r', encoding='utf-8') alllines = f.readlines() f.close() f = open(filename, 'w+', encoding='utf-8') for eachline in alllines: a = re.sub(subp[i], ' ', eachline) f.writelines(a) f.close() f = open(filename, 'r', encoding='utf-8') alllines = f.readlines() f.close() f = open(filename, 'w+', encoding='utf-8') for eachline in alllines: a = re.sub('Itoda', '伊藤\nItoda', eachline) f.writelines(a) f.close()
def myrandom(): p = random.randint(1, 12652) if p % 2 == 0: q = p p = p - 1 else: q = p + 1 prtl1 = linecache.getline('result.txt', p) prtl2 = linecache.getline('result.txt', q) print("罗马音:", prtl1, "名称:", prtl2)
print("---欢迎使用随机命名工具,本工具会在相同目录下创建列表,并根据需求随机给出日语名称及对应罗马音---\n") while True: flag = int(input("---输入1以获取列表---\n""---输入2以随机获得名称---\n---输入0以退出---\n---请输入:")) path = os.path.exists('result.txt') if flag == 1: if path==True: print("---您已获取过列表---\n") else: print("---这可能需要1分钟时间,请耐心等候---") gethtml() correct() print("---获取完成---\n") if flag == 2: if path == True: myrandom() else: print("---请先获取列表---\n") if flag == 0: break print("---进程已结束---")
|