jeremyjia / Games

Home Page Link:
https://jeremyjia.github.io/Games/
3 stars 9 forks source link

古诗词填空 #946

Open jeremyjia opened 1 year ago

jeremyjia commented 1 year ago

需求来源:这是给一个高中生写的程序设计作业 代码地址:https://jeremyjia.github.io/Games/python/sample/poery/easyGUI.py

jeremyjia commented 1 year ago

GUI参考:

  1. https://blog.csdn.net/weixin_44289254/article/details/121043821
  2. https://easygui.readthedocs.io/en/master/ (官方文档)
jeremyjia commented 1 year ago

在线翻译程序备份,与此需求无关

循环

while True:

url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
#去掉了原url中的_o  translate_o?smartresult
#当url中包含"_o"的时候会自动包含如下两个参数:

#salt: 15865740020664
#sign: e6b1f91edcff2995cd860e3bc205009d

#salt应该是根据系统时间生成的一个时间戳,
#sign是以要查询的数据和salt生成的加密数据,每次查询新的单词或者中文时都需要把值和新的salt传给加密公式进行加密,因此每次的sign都不一样,导致出现{“errorCode”:50}错误。

content = g.enterbox(msg='请输入要翻译的内容', title='翻译小程序')

如果输入内容为空,则重新输入

while content=='':
    content = g.enterbox(msg='您没有输入内容,请重新输入', title='翻译小程序')

如果取消则程序退出

if content == None:
    g.msgbox('程序退出')
    sys.exit()

伪装头部 方法一:需要urllib.request.Request传入head resp = urllib.request.Request(url,data,head)

head={}

#head['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'

设置data

data = {}
data['i'] = content
data['from']='AUTO'
data['to']='AUTO'
data['smartresult']='dict'
data['client'] = 'fanyideskweb'
#data['salt']='15865740020664'
#data['sign']='e6b1f91edcff2995cd860e3bc205009d'
#data['ts']='1586574002066'
#data['bv']= '1acba1212ecd71259407b21061693d8a'
data['doctype']='json'
data['version']='2.1'
data['keyfrom']='fanyi.web'
data['action']='FY_BY_REALTlME'

解析

data = urllib.parse.urlencode(data).encode(encoding='utf-8')

伪装头部 方法二:建请求对象(包装请求,重构User-Agent,使程序更像正常人类请求)

resp = urllib.request.Request(url,data)
resp.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36')

获取

response = urllib.request.urlopen(resp)
html = response.read().decode('utf-8')
target = json.loads(html)

i = content + '的翻译结果是:' + (target['translateResult'][0][0]['tgt'])
g.msgbox(i,'翻译小程序')
if g.ccbox('要重新开始吗','确认'):
    pass
else:
    sys.exit()
jeremyjia commented 1 year ago

参考:古诗词游戏,控制台版本: https://blog.csdn.net/qq_42965915/article/details/108346932

jeremyjia commented 11 months ago

古诗词背诵程序设计.docx