【项目4】词典软件
1 # 1. 介绍 json 2 # 下面是一个存储了单词 name 的释义的 json 格式字符串 3 # 你可以简单地把它看为一个字典,并且我们可以用标准库 json 轻松把这个字符串转为 python dict 4 # 它的结构和字典一样 5 6 # 2. 介绍数据含义 7 # 这个 释义 是从爱词霸的服务器上获取的,我们先不管获取的方法,先关心里面存储的数据的含义 8 # word_name 是被查询的单词 9 # is_CRI 不需要关心,可以忽略 10 # exchange 是一个字典,包含了单词的所有形式(复数、过去时等) 11 # symbols 是一个数组,目前里面只包含了一个字典 12 # 其中有 3 个 mp3 文件的链接,分别是英音、美音、语音合成音 13 # parts 是一个数组,里面包含了单词 name 所有的含义 14 import urllib.request 15 import json 16 """ 17 { 18 "word_name": "name", 19 "is_CRI": "1", 20 "exchange": { 21 "word_pl": [ 22 "names" 23 ], 24 "word_third": [ 25 "names" 26 ], 27 "word_past": [ 28 "named" 29 ], 30 "word_done": [ 31 "named" 32 ], 33 "word_ing": [ 34 "naming" 35 ], 36 "word_er": "", 37 "word_est": "" 38 }, 39 "symbols": [{ 40 "ph_en_mp3": "http://res.iciba.com/resource/amp3/oxford/0/1b/c3/1bc38ba928f40072e7c62d427a05c03e.mp3", 41 "ph_am_mp3": "http://res.iciba.com/resource/amp3/1/0/b0/68/b068931cc450442b63f5b3d276ea4297.mp3", 42 "ph_tts_mp3": "http://res-tts.iciba.com/b/0/6/b068931cc450442b63f5b3d276ea4297.mp3", 43 "parts": [{ 44 "part": "n.", 45 "means": [ 46 "名字", 47 "名声", 48 "有…名称的", 49 "著名的人物" 50 ] 51 }, 52 { 53 "part": "vt.", 54 "means": [ 55 "确定", 56 "决定", 57 "给…取名", 58 "说出…的名字" 59 ] 60 }, 61 { 62 "part": "adj.", 63 "means": [ 64 "著名的", 65 "据以取名" 66 ] 67 } 68 ] 69 }] 70 } 71 """ 72 73 # 3. 使用 json 标准库解析 json 格式字符串为 python 的 dict 74 """ 75 import json 76 77 78 s = ''' 79 { 80 "name": "gua", 81 "height": [ 82 169, 83 1.69 84 ] 85 } 86 ''' 87 88 d = json.loads(s) 89 90 print('result', d['name'], d['height'][1]) 91 # 结果如下 92 # result gua 1.69 93 """ 94 95 96 # 4. 获取单词释义的 json 格式字符串 97 """ 98 1) 打开爱词霸网站,链接如下 99 http://open.iciba.com/?c=api 100 101 2)选择词霸查词 102 输入网址名称、网址和你的邮箱地址(网址名称和网址可随意填写) 103 提交后邮箱会收到一个邮件包含身份 key 104 105 3)用如下的代码获取到单词释义的 json 格式字符串 106 # 输入你的身份 key 107 key = '' 108 word = 'name' 109 url = 'http://dict-co.iciba.com/api/dictionary.php?type=json&key={}&w={}'.format(key, word) 110 111 # openurl 是课 4 作业 14 的 openurl 函数 112 # 用来获取网络词典返回的结果 113 s = openurl(url) 114 """ 115 116 """ 117 作业要求: 118 本作业需要按照上面的顺序描述实现一个词典软件 119 实现 translate 函数,输出所有的单词含义 120 例如对于 name 这个单词,输出如下 121 n. 122 名字 123 名声 124 有…名称的 125 著名的人物 126 127 vt. 128 确定 129 决定 130 给…取名 131 说出…的名字 132 133 adj. 134 著名的 135 据以取名 136 """ 137 138 139 log = print 140 141 142 def openurl(url): 143 # 下载页面, 得到的是一个 bytes 类型的变量 s 144 s = urllib.request.urlopen(url).read() 145 # 用 utf-8 编码把 s 转为字符串并返回 146 content = s.decode('utf-8') 147 return content 148 149 150 def translate(word): 151 """ 152 word 是一个不包含空格的单词 153 """ 154 key = '3C2D3651D97914DC028F61B2C4F883C0' 155 url = 'http://dict-co.iciba.com/api/dictionary.php?type=json&key={}&w={}'.format( 156 key, word) 157 s = openurl(url) 158 d = json.loads(s) 159 d = d['symbols'][0]['parts'] 160 result = [] 161 for i in d: 162 key = i['part'] 163 value = i['means'] 164 result.append(key) 165 for j in range(len(value)): 166 result.append(value[j]) 167 result.append('\n') 168 for i in result: 169 if i == '\n': 170 print(i, end="") 171 else: 172 print(i) 173 # return result 174 175 176 def main(): 177 word = 'mother' 178 translate(word) 179 180 181 if __name__ == '__main__': 182 main()