开启辅助访问      

站内搜索

搜索
热搜: 下载 1.19 1.20

Minecraft(我的世界)苦力怕论坛

[其他] [非mc]Python手搓全球发音

发表于 2024-3-5 20:26:58 | 显示全部楼层 |阅读模式 IP:福建省
本帖最后由 daijunhao 于 2024-3-6 17:43 编辑

# 前言
最近全球发音这东西挺搞笑的(gee)
但是网易有道词典中的全球发音要下载客户端才有

image.png
客户端要200多MB
image.png
那不妨来试试python弄出来的全球发音吧(用的是网易有道词典的api,应该算吧)
# 原理分析
首先是有道词典获取全球发音的内容
https://dict.youdao.com/mvoice?method=getInfo&word=gee

"gee"是一个单词
访问之后我们就能看到json
  1. {
  2.     "attributes": {
  3.         "total": 4,
  4.         "headword": "gee",
  5.         "source_zh": "英语",
  6.         "source": "English",
  7.         "langNumber": 3
  8.     },
  9.     "items": [
  10.         {
  11.             "headword": "gee",
  12.             "voices": [
  13.                 {
  14.                     "country": "China",
  15.                     "voiceId": 1762315453293305900,
  16.                     "voiceId_str": "1762315453293305856",
  17.                     "sex": "m",
  18.                     "location": "",
  19.                     "votes": 83643,
  20.                     "oppose": 2076,
  21.                     "id": 1709003841851,
  22.                     "country_zh": "中国"
  23.                 },
  24.                 {
  25.                     "country": "United Kingdom",
  26.                     "voiceId": 719288437215584000,
  27.                     "voiceId_str": "719288437215583939",
  28.                     "sex": "m",
  29.                     "location": "55.378051,-3.435973",
  30.                     "votes": 2282,
  31.                     "oppose": 1629,
  32.                     "id": 767804,
  33.                     "country_zh": "英国"
  34.                 }
  35.             ],
  36.             "lang-zh": "英语",
  37.             "lang": "English"
  38.         },
  39.         {
  40.             "headword": "gee",
  41.             "voices": [
  42.                 {
  43.                     "country": "South Africa",
  44.                     "voiceId": 419445984134648960,
  45.                     "voiceId_str": "419445984134648974",
  46.                     "sex": "m",
  47.                     "location": "-30.559482,22.937506",
  48.                     "votes": 444,
  49.                     "oppose": 775,
  50.                     "id": 2666578,
  51.                     "country_zh": "南非"
  52.                 }
  53.             ],
  54.             "lang-zh": "南非语",
  55.             "lang": "Afrikaans"
  56.         },
  57.         {
  58.             "headword": "gee",
  59.             "voices": [
  60.                 {
  61.                     "country": "United Kingdom",
  62.                     "voiceId": 155786722392261400,
  63.                     "voiceId_str": "155786722392261414",
  64.                     "sex": "m",
  65.                     "location": "55.378051,-3.435973",
  66.                     "votes": 403,
  67.                     "oppose": 514,
  68.                     "id": 5731589,
  69.                     "country_zh": "英国"
  70.                 }
  71.             ],
  72.             "lang-zh": "马恩岛语",
  73.             "lang": "Manx"
  74.         }
  75.     ]
  76. }
复制代码
在这json里面
"sex"这个值中代表的是发声性别
比如里面的值是"m"就代表是男性发声
里面的值是"f"就代表是女性发声
"country"为国家,就是发音人所在的国家
"votes"为投票数
其中里面的"voiceId_str"这个值就是发音的关键了
然后我们再获取音频内容
https://dict.youdao.com/mvoice?method=getVoice&id=1762315453293305856

其中"1762315453293305856"就是我们获取到的"voiceId_str"
访问这段内容,就可以看到音频了


然后就是有道词典的英式发音和美式发音

先是英式发音
https://dict.youdao.com/dictvoice?audio=gee&type=1

其中这里面的"gee"为单词
访问这段内容,就可以听到英式发音


然后是美式发音
https://dict.youdao.com/dictvoice?audio=gee&type=2

其中这里面的"gee"为单词
访问这段内容,就可以听到美式发音


# 代码实现
使用python(需要安装wxpython requests io soundfile sounddevice库)
  1. # -*- coding:utf-8 -*-
  2. import wx
  3. import requests
  4. import json
  5. from io import BytesIO
  6. import sounddevice as sd
  7. import soundfile as sf
  8. #忽略证书警告
  9. requests.packages.urllib3.disable_warnings()
  10. headers = {
  11.     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0'
  12. }
  13. class Frame(wx.Frame):
  14.     def __init__(self):
  15.         wx.Frame.__init__(self, None, title='全球发音--网易有道词典 by:daijunhao', size=(1200, 800),name='frame',style=541072384)
  16.         self.启动窗口 = wx.Panel(self)
  17.         self.Centre()
  18.         self.超级列表框1 = wx.ListCtrl(self.启动窗口, size=(1109, 380), pos=(25, 28), name='listCtrl', style=8227)
  19.         self.超级列表框1.AppendColumn('序号', 0, 154)
  20.         self.超级列表框1.AppendColumn('发音单词', 0, 109)
  21.         self.超级列表框1.AppendColumn('发音语言', 0, 151)
  22.         self.超级列表框1.AppendColumn('投票数', 0, 171)
  23.         self.超级列表框1.AppendColumn('音频id', 0, 490)
  24.         self.超级列表框1.Bind(wx.EVT_LIST_ITEM_SELECTED,self.超级列表框1_选中表项)
  25.         self.编辑框1 = wx.TextCtrl(self.启动窗口, size=(336, 22), pos=(117, 438), value='', name='text', style=0)
  26.         self.标签1 = wx.StaticText(self.启动窗口, size=(80, 24), pos=(25, 437), label='请输入单词:', name='staticText',style=2321)
  27.         self.按钮1 = wx.Button(self.启动窗口, size=(80, 32), pos=(25, 474), label='查询单词', name='button')
  28.         self.按钮1.Bind(wx.EVT_BUTTON, self.按钮1_按钮被单击)
  29.         self.按钮2 = wx.Button(self.启动窗口, size=(171, 81), pos=(20, 667), label='下载音频(全球发音)', name='button')
  30.         self.按钮2.Bind(wx.EVT_BUTTON, self.按钮2_按钮被单击)
  31.         self.按钮3 = wx.Button(self.启动窗口, size=(171, 81), pos=(20, 571), label='音频朗读(全球发音)', name='button')
  32.         self.按钮3.Bind(wx.EVT_BUTTON, self.按钮3_按钮被单击)
  33.         self.按钮4 = wx.Button(self.启动窗口, size=(80, 32), pos=(1054, 416), label='刷新', name='button')
  34.         self.按钮4.Bind(wx.EVT_BUTTON, self.按钮4_按钮被单击)
  35.         self.按钮5 = wx.Button(self.启动窗口, size=(171, 81), pos=(410, 667), label='英式发音', name='button')
  36.         self.按钮5.Bind(wx.EVT_BUTTON, self.按钮5_按钮被单击)
  37.         self.按钮6 = wx.Button(self.启动窗口, size=(171, 81), pos=(410, 571), label='美式发音', name='button')
  38.         self.按钮6.Bind(wx.EVT_BUTTON, self.按钮6_按钮被单击)
  39.         self.按钮7 = wx.Button(self.启动窗口, size=(171, 81), pos=(214, 571), label='下载美式发音', name='button')
  40.         self.按钮7.Bind(wx.EVT_BUTTON, self.按钮7_按钮被单击)
  41.         self.按钮8 = wx.Button(self.启动窗口, size=(171, 81), pos=(214, 667), label='下载英式发音', name='button')
  42.         self.按钮8.Bind(wx.EVT_BUTTON, self.按钮8_按钮被单击)
  43.         self.按钮2.Hide()
  44.         self.按钮3.Hide()

  45.         self.按钮8.Hide()
  46.         self.按钮7.Hide()
  47.         self.按钮6.Hide()
  48.         self.按钮5.Hide()


  49.     def 超级列表框1_选中表项(self,event):
  50.         self.按钮2.Enable()
  51.         self.按钮3.Enable()


  52.     def 按钮1_按钮被单击(self,event):
  53.         self.按钮8.Show()
  54.         self.按钮7.Show()
  55.         self.按钮6.Show()
  56.         self.按钮5.Show()
  57.         mvoice_get_list = json.loads(requests.get(f"https://dict.youdao.com/mvoice?method=getInfo&word={str(self.编辑框1.GetValue())}",headers=headers,verify=False).text)
  58.         self.超级列表框1.ClearAll()
  59.         self.按钮2.Disable()
  60.         self.按钮3.Disable()
  61.         self.超级列表框1.AppendColumn('序号', 0, 154)
  62.         self.超级列表框1.AppendColumn('发音单词', 0, 109)
  63.         self.超级列表框1.AppendColumn('发音语言', 0, 151)
  64.         self.超级列表框1.AppendColumn('投票数', 0, 171)
  65.         self.超级列表框1.AppendColumn('音频id', 0, 490)
  66.         voice_count = 1
  67.         try:
  68.             self.按钮2.Show()
  69.             self.按钮3.Show()
  70.             for item in mvoice_get_list['items']:
  71.                 for mvoice_get_list1 in item['voices']:
  72.                     if str(mvoice_get_list1["sex"]) == "f":
  73.                         self.超级列表框1.Append([f'{str(voice_count)}.{mvoice_get_list1["country_zh"]}/女', f'{str(mvoice_get_list["attributes"]["headword"])}', f'{str(item["lang-zh"])}', f'{str(mvoice_get_list1["votes"])}', f'{str(mvoice_get_list1["voiceId_str"])}'])
  74.                     if str(mvoice_get_list1["sex"]) == "m":
  75.                         self.超级列表框1.Append([f'{str(voice_count)}.{mvoice_get_list1["country_zh"]}/男', f'{str(mvoice_get_list["attributes"]["headword"])}', f'{str(item["lang-zh"])}', f'{str(mvoice_get_list1["votes"])}', f'{str(mvoice_get_list1["voiceId_str"])}'])
  76.                     voice_count += 1
  77.             voice_info = wx.MessageDialog(None, caption="info",message=f"获取完毕,该单词有{str(voice_count-1)}位用户贡献了发音",style=wx.OK | wx.ICON_INFORMATION)
  78.             if voice_info.ShowModal() == wx.ID_OK:
  79.                 pass
  80.         except Exception:
  81.             self.按钮2.Disable()
  82.             self.按钮3.Disable()
  83.             self.按钮2.Hide()
  84.             self.按钮3.Hide()

  85.             self.按钮8.Hide()
  86.             self.按钮7.Hide()
  87.             self.按钮6.Hide()
  88.             self.按钮5.Hide()
  89.             voice_error = wx.MessageDialog(None, caption="Error",message="获取失败,请检查你的单词是否有用户贡献发音",style=wx.OK | wx.ICON_ERROR)
  90.             if voice_error.ShowModal() == wx.ID_OK:
  91.                 pass


  92.     def 按钮3_按钮被单击(self,event):
  93.         try:
  94.             mvoice_get_list = json.loads(requests.get(f"https://dict.youdao.com/mvoice?method=getInfo&word={str(self.编辑框1.GetValue())}",headers=headers,verify=False).text)
  95.             voice_count = 1
  96.             for item in mvoice_get_list['items']:
  97.                 for mvoice_get_list1 in item['voices']:
  98.                     if voice_count == self.超级列表框1.GetFirstSelected()+1:
  99.                         mvoice_get_mp3 = requests.get(f"https://dict.youdao.com/mvoice?method=getVoice&id={str(mvoice_get_list1['voiceId_str'])}",headers=headers,verify=False).content
  100.                         filename = BytesIO(mvoice_get_mp3)
  101.                         # Extract data and sampling rate from file
  102.                         data, fs = sf.read(filename, dtype='float32')
  103.                         sd.play(data, fs)
  104.                         status = sd.wait()  # Wait until file is done playing
  105.                     voice_count += 1
  106.             voice_playsound_info = wx.MessageDialog(None, caption="info", message="朗读完毕",style=wx.OK | wx.ICON_INFORMATION)
  107.             if voice_playsound_info.ShowModal() == wx.ID_OK:
  108.                 pass
  109.         except Exception:
  110.             voice_playsound_error = wx.MessageDialog(None, caption="Error", message="朗读失败,请检查你输入的单词是否为空", style=wx.OK | wx.ICON_ERROR)
  111.             if voice_playsound_error.ShowModal() == wx.ID_OK:
  112.                 pass


  113.     def 按钮2_按钮被单击(self,event):
  114.         try:
  115.             mvoice_get_list = json.loads(requests.get(f"https://dict.youdao.com/mvoice?method=getInfo&word={str(self.编辑框1.GetValue())}",headers=headers,verify=False).text)
  116.             voice_count = 1
  117.             for item in mvoice_get_list['items']:
  118.                 for mvoice_get_list1 in item['voices']:
  119.                     if voice_count == self.超级列表框1.GetFirstSelected()+1:
  120.                         mvoice_get_mp3 = requests.get(f"https://dict.youdao.com/mvoice?method=getVoice&id={str(mvoice_get_list1['voiceId_str'])}",headers=headers,verify=False).content
  121.                     voice_count += 1
  122.             fileFilter = "mp3 Files (*.mp3)|*.mp3|" "All files (*.*)|*.*"
  123.             fileDialog = wx.FileDialog(self, message="保存此mp3文件", wildcard=fileFilter, style=wx.FD_SAVE)
  124.             dialogResult = fileDialog.ShowModal()
  125.             if dialogResult == wx.ID_OK:
  126.                 with open(f"{fileDialog.GetPath()}",mode="wb") as f:
  127.                     f.write(mvoice_get_mp3)
  128.                     f.close()
  129.                 voice_save_ok = wx.MessageDialog(None, caption="info", message="保存成功",style=wx.OK | wx.ICON_INFORMATION)
  130.                 if voice_save_ok.ShowModal() == wx.ID_OK:
  131.                     pass
  132.         except Exception:
  133.             voice_save_error = wx.MessageDialog(None, caption="Error", message="保存失败,请检查你输入的单词是否为空", style=wx.OK | wx.ICON_ERROR)
  134.             if voice_save_error.ShowModal() == wx.ID_OK:
  135.                 pass


  136.     def 按钮4_按钮被单击(self, event):
  137.             mvoice_get_list = json.loads(requests.get(f"https://dict.youdao.com/mvoice?method=getInfo&word={str(self.编辑框1.GetValue())}",headers=headers,verify=False).text)
  138.             self.超级列表框1.ClearAll()
  139.             self.按钮2.Disable()
  140.             self.按钮3.Disable()
  141.             self.超级列表框1.AppendColumn('序号', 0, 154)
  142.             self.超级列表框1.AppendColumn('发音单词', 0, 109)
  143.             self.超级列表框1.AppendColumn('发音语言', 0, 151)
  144.             self.超级列表框1.AppendColumn('投票数', 0, 171)
  145.             self.超级列表框1.AppendColumn('音频id', 0, 490)
  146.             voice_count = 1
  147.             try:
  148.                 self.按钮2.Show()
  149.                 self.按钮3.Show()
  150.                 for item in mvoice_get_list['items']:
  151.                     for mvoice_get_list1 in item['voices']:
  152.                         if str(mvoice_get_list1["sex"]) == "f":
  153.                             self.超级列表框1.Append([f'{str(voice_count)}.{mvoice_get_list1["country_zh"]}/女', f'{str(mvoice_get_list["attributes"]["headword"])}', f'{str(item["lang-zh"])}', f'{str(mvoice_get_list1["votes"])}', f'{str(mvoice_get_list1["voiceId_str"])}'])
  154.                         if str(mvoice_get_list1["sex"]) == "m":
  155.                             self.超级列表框1.Append([f'{str(voice_count)}.{mvoice_get_list1["country_zh"]}/男', f'{str(mvoice_get_list["attributes"]["headword"])}', f'{str(item["lang-zh"])}', f'{str(mvoice_get_list1["votes"])}', f'{str(mvoice_get_list1["voiceId_str"])}'])
  156.                         voice_count += 1
  157.                 voice_info = wx.MessageDialog(None, caption="info",message=f"获取完毕,该单词有{str(voice_count-1)}位用户贡献了发音",style=wx.OK | wx.ICON_INFORMATION)
  158.                 if voice_info.ShowModal() == wx.ID_OK:
  159.                     pass
  160.             except Exception:
  161.                 self.按钮2.Disable()
  162.                 self.按钮3.Disable()
  163.                 self.按钮2.Hide()
  164.                 self.按钮3.Hide()
  165.                 voice_error = wx.MessageDialog(None, caption="Error",message="获取失败,请检查你的单词是否有用户贡献发音",style=wx.OK | wx.ICON_ERROR)
  166.                 if voice_error.ShowModal() == wx.ID_OK:
  167.                     pass

  168.     def 按钮5_按钮被单击(self, event):
  169.         try:
  170.             mvoice_get_mp3 = requests.get(f"https://dict.youdao.com/dictvoice?audio={str(self.编辑框1.GetValue())}&type=1",headers=headers,verify=False).content
  171.             filename = BytesIO(mvoice_get_mp3)
  172.             # Extract data and sampling rate from file
  173.             data, fs = sf.read(filename, dtype='float32')
  174.             sd.play(data, fs)
  175.             status = sd.wait()  # Wait until file is done playing
  176.             voice_playsound_info = wx.MessageDialog(None, caption="info", message="朗读完毕",style=wx.OK | wx.ICON_INFORMATION)
  177.             if voice_playsound_info.ShowModal() == wx.ID_OK:
  178.                 pass
  179.         except Exception:
  180.             voice_playsound_error = wx.MessageDialog(None, caption="Error", message="朗读失败,请检查你输入的单词是否为空", style=wx.OK | wx.ICON_ERROR)
  181.             if voice_playsound_error.ShowModal() == wx.ID_OK:
  182.                 pass

  183.     def 按钮6_按钮被单击(self, event):
  184.         try:
  185.             mvoice_get_mp3 = requests.get(f"https://dict.youdao.com/dictvoice?audio={str(self.编辑框1.GetValue())}&type=2",headers=headers,verify=False).content
  186.             filename = BytesIO(mvoice_get_mp3)
  187.             # Extract data and sampling rate from file
  188.             data, fs = sf.read(filename, dtype='float32')
  189.             sd.play(data, fs)
  190.             status = sd.wait()  # Wait until file is done playing
  191.             voice_playsound_info = wx.MessageDialog(None, caption="info", message="朗读完毕",style=wx.OK | wx.ICON_INFORMATION)
  192.             if voice_playsound_info.ShowModal() == wx.ID_OK:
  193.                 pass
  194.         except Exception:
  195.             voice_playsound_error = wx.MessageDialog(None, caption="Error", message="朗读失败,请检查你输入的单词是否为空", style=wx.OK | wx.ICON_ERROR)
  196.             if voice_playsound_error.ShowModal() == wx.ID_OK:
  197.                 pass

  198.     def 按钮7_按钮被单击(self, event):
  199.         mvoice_get_mp3 = requests.get(f"https://dict.youdao.com/dictvoice?audio={str(self.编辑框1.GetValue())}&type=2",headers=headers,verify=False).content
  200.         fileFilter = "mp3 Files (*.mp3)|*.mp3|" "All files (*.*)|*.*"
  201.         fileDialog = wx.FileDialog(self, message="保存此mp3文件", wildcard=fileFilter, style=wx.FD_SAVE)
  202.         dialogResult = fileDialog.ShowModal()
  203.         if dialogResult == wx.ID_OK:
  204.             with open(f"{fileDialog.GetPath()}",mode="wb") as f:
  205.                 f.write(mvoice_get_mp3)
  206.                 f.close()
  207.             voice_save_ok = wx.MessageDialog(None, caption="info", message="保存成功",style=wx.OK | wx.ICON_INFORMATION)
  208.             if voice_save_ok.ShowModal() == wx.ID_OK:
  209.                 pass

  210.     def 按钮8_按钮被单击(self, event):
  211.         mvoice_get_mp3 = requests.get(f"https://dict.youdao.com/dictvoice?audio={str(self.编辑框1.GetValue())}&type=1",headers=headers,verify=False).content
  212.         fileFilter = "mp3 Files (*.mp3)|*.mp3|" "All files (*.*)|*.*"
  213.         fileDialog = wx.FileDialog(self, message="保存此mp3文件", wildcard=fileFilter, style=wx.FD_SAVE)
  214.         dialogResult = fileDialog.ShowModal()
  215.         if dialogResult == wx.ID_OK:
  216.             with open(f"{fileDialog.GetPath()}",mode="wb") as f:
  217.                 f.write(mvoice_get_mp3)
  218.                 f.close()
  219.             voice_save_ok = wx.MessageDialog(None, caption="info", message="保存成功",style=wx.OK | wx.ICON_INFORMATION)
  220.             if voice_save_ok.ShowModal() == wx.ID_OK:
  221.                 pass

  222. class myApp(wx.App):
  223.     def OnInit(self):
  224.         self.frame = Frame()
  225.         self.frame.Show(True)
  226.         return True

  227. if __name__ == '__main__':
  228.     app = myApp()
  229.     app.MainLoop()
复制代码
图形化界面:
image.png
下载链接:https://wwz.lanzouo.com/i1TXr1qipxla密码:2da5






苦力怕论坛,感谢有您~
回复

使用道具 举报

发表于 2024-3-5 20:35:33 | 显示全部楼层 IP:广东省
瓦 好力害啊 python难吗
2# 2024-3-5 20:35:33 回复 收起回复
苦力怕论坛,感谢有您~
回复 支持

使用道具 举报

头像被屏蔽
发表于 2024-3-5 22:16:04 来自手机 | 显示全部楼层 IP:江苏省
程序很好,但是被“网易”劝退
3# 2024-3-5 22:16:04 回复 收起回复
苦力怕论坛,感谢有您~
回复 支持

使用道具 举报

 楼主| 发表于 2024-3-6 17:22:12 | 显示全部楼层 IP:福建省
本帖最后由 daijunhao 于 2024-3-6 17:40 编辑

话说回来(草稿这东西还会自动发出去,下次直接粘贴到word文档里了)
4# 2024-3-6 17:22:12 回复 收起回复
苦力怕论坛,感谢有您~
回复 支持

使用道具 举报

 楼主| 发表于 2024-3-6 17:30:28 | 显示全部楼层 IP:福建省
limil_23 发表于 2024-3-5 20:35
瓦 好力害啊 python难吗

只有单独命令提示符还可以,图形化界面的话...
5# 2024-3-6 17:30:28 回复 收起回复
苦力怕论坛,感谢有您~
回复 支持

使用道具 举报

发表于 2024-3-8 23:07:45 | 显示全部楼层 IP:上海
大佬nb 搞得我都想学Python了((
6# 2024-3-8 23:07:45 回复 收起回复
苦力怕论坛,感谢有您~
回复 支持

使用道具 举报

发表于 3 天前 来自手机 | 显示全部楼层 IP:广东省
6666666666666666
7# 3 天前 回复 收起回复
苦力怕论坛,感谢有您~
回复 支持

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

本站
关于我们
联系我们
坛史纲要
官方
哔哩哔哩
技术博客
下载
网易版
安卓版
JAVA
反馈
意见建议
教程中心
更多
捐助本站
QQ群
QQ群

QQ群

访问手机版

访问手机版

手机版|小黑屋|系统状态|klpbbs.com

粤公网安备 44200002445329号 | 由 木韩网络 提供云服务 | GMT+8, 2024-4-27 15:51

声明:本站与Mojang以及微软公司没有从属关系

Powered by Discuz! X3.4 粤ICP备2023071842号