鱼C论坛

 找回密码
 立即注册
查看: 2294|回复: 6

[作品展示] 简易英语词典

[复制链接]
发表于 2020-2-8 18:21:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 suchocolate 于 2020-2-8 19:10 编辑

利用request,tkinter等技术制作的简易英文字典:

                               
登录/注册后可看大图

                               
登录/注册后可看大图
  1. import requests
  2. from lxml import etree
  3. from tkinter import *
  4. from urllib.parse import quote

  5. # 英文查询
  6. ebase = 'http://dict.youdao.com/w/eng/'
  7. # 中文查询
  8. cbase = 'http://dict.youdao.com/w/'
  9. # 查询时http头
  10. headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"}


  11. # 查询函数
  12. def chaxun():
  13.     # 获取输入框内容
  14.     wd = e1.get()
  15.     # 分析输入框内容
  16.     if '\u4e00' <= wd <= '\u9fff':
  17.         # 如果是中文查询,转换中文为URL编码格式
  18.         wd = quote(wd)
  19.         url = cbase + wd
  20.         r = requests.get(url, headers=headers)
  21.         html = etree.HTML(r.text)
  22.         # 获取查询结果
  23.         result = html.xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/p/span/a/text()')
  24.     else:
  25.         url = ebase + wd
  26.         r = requests.get(url, headers=headers)
  27.         html = etree.HTML(r.text)
  28.         result = html.xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/li/text()')
  29.     # 清理text,重新显示
  30.     text1.delete(1.0, END)
  31.     # 如果有结果,显示结果
  32.     if len(result) != 0:
  33.         for pt in result:
  34.             text1.insert(INSERT, pt + '\n')
  35.     else:
  36.         # 如果没有结果,提示没有结果
  37.         text1.insert(INSERT, 'There is no explain.')


  38. if __name__ == '__main__':
  39.     # 主函数,定义一个tk对象,设置标题
  40.     root = Tk()
  41.     root.title('简易有道字典')
  42.     # 文字提示
  43.     l1 = Label(root, text='请输入要查询的单词:')
  44.     l1.grid(row=0)
  45.     # 添加一个输入框
  46.     e1 = Entry(root)
  47.     e1.grid(row=0, column=1, padx=10, pady=5)
  48.     # 添加一个按钮,点击时执行查询程序
  49.     bt1 = Button(root, text='查询', command=chaxun)
  50.     bt1.grid(row=0, column=2, padx=10, pady=5)
  51.      # 定义一个文本框,展示查询结果
  52.     text1 = Text(root, width=45, height=10)
  53.     text1.grid(row=1, columnspan=3)
  54.     # 主窗体循环
  55.     mainloop()
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
zltzlt + 5 + 5

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-8 18:42:08 | 显示全部楼层
不错不错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-8 21:35:52 | 显示全部楼层
本帖最后由 suchocolate 于 2020-2-8 21:37 编辑

进阶带翻译版

fy

fy

  1. from urllib import request, parse
  2. from urllib.parse import quote
  3. from lxml import etree
  4. from tkinter import *
  5. import json


  6. # 英文单词查询网址
  7. ebase = 'http://dict.youdao.com/w/eng/'
  8. # 中文单词查询网址
  9. cbase = 'http://dict.youdao.com/w/'
  10. # 翻译网址
  11. trans = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
  12. # 查询时http头
  13. headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"}
  14. # 翻译提交的字典
  15. dic = {"doctype": "json"}


  16. # 判断查询类型
  17. def chaxun():
  18.     if v.get() == 1:
  19.         chaci()
  20.     else:
  21.         fanyi()


  22. # 查词 chaci():
  23. def chaci():
  24.     wd = e1.get()
  25.     if '\u4e00' <= wd <= '\u9fff':
  26.         # 如果是中文查询,转换中文为URL编码格式
  27.         wd = quote(wd)
  28.         url = cbase + wd
  29.         q = request.Request(url=url, headers=headers)
  30.         r = request.urlopen(q, timeout=2)
  31.         html = etree.HTML(r.read().decode('utf-8'))
  32.         result = html.xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/p/span/a/text()')
  33.     else:
  34.         url = ebase + wd
  35.         q = request.Request(url=url, headers=headers)
  36.         r = request.urlopen(q, timeout=2)
  37.         html = etree.HTML(r.read().decode('utf-8'))
  38.         result = html.xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/li/text()')
  39.     # 清理text,重新显示
  40.     text1.delete(1.0, END)
  41.     # 如果有结果,显示结果
  42.     if len(result) != 0:
  43.         for pt in result:
  44.             text1.insert(INSERT, pt + '\n')
  45.     else:
  46.         # 如果没有结果,提示没有结果
  47.         text1.insert(INSERT, 'There is no explain.')


  48. # 翻译
  49. def fanyi():
  50.     text1.delete(1.0, END)
  51.     wd = e1.get()
  52.     dic['i'] = wd
  53.     data = bytes(parse.urlencode(dic), encoding='utf-8')
  54.     q = request.Request(url=trans, data=data, headers=headers, method='POST')
  55.     r = request.urlopen(q)
  56.     result = json.loads(r.read().decode('utf-8'))
  57.     text1.insert(INSERT, result['translateResult'][0][0]['tgt'])


  58. if __name__ == '__main__':
  59.     # 主函数,定义一个tk对象
  60.     root = Tk()
  61.     root.title('简易有道字典')
  62.     l1 = Label(root, text='请输入要查询的内容:')
  63.     l1.grid(row=0, column=0)
  64.     e1 = Entry(root)
  65.     e1.grid(row=0, column=1, padx=1, pady=5)
  66.     bt1 = Button(root, text='查询', command=chaxun)
  67.     bt1.grid(row=0, column=2, padx=5, pady=5)
  68.     text1 = Text(root, width=59, height=10)
  69.     text1.grid(row=1, columnspan=5, padx=5, pady=7)
  70.     v = IntVar()
  71.     v.set(1)
  72.     Radiobutton(root, text="查词", variable=v, value=1).grid(row=0, column=3)
  73.     Radiobutton(root, text="翻译", variable=v, value=2).grid(row=0, column=4)
  74.     mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-21 14:53:59 | 显示全部楼层
在python3.8  64-bit里跑,lxml要改成xml
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-21 20:17:58 | 显示全部楼层
本帖最后由 suchocolate 于 2020-3-21 20:24 编辑

Nelson盟新 发表于 2020-3-21 14:53
在python3.8  64-bit里跑,lxml要改成xml


                               
登录/注册后可看大图

我这里python 3.8.2 64位,没有要求改。
另外我查了下lxml官网api手册,没说改名,还是这个方式导入:https://lxml.de/api.html

                               
登录/注册后可看大图


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-21 22:49:49 | 显示全部楼层
本帖最后由 Nelson盟新 于 2020-3-21 22:55 编辑

用这个查的
https://github.com/python/cpython/tree/3.8/Lib/

  1. XML tree and elements
  2. XML is an inherently hierarchical data format, and the most natural way to represent it is with a tree. ET has two classes for this purpose - ElementTree represents the whole XML document as a tree, and Element represents a single node in this tree. Interactions with the whole document (reading and writing to/from files) are usually done on the ElementTree level. Interactions with a single XML element and its sub-elements are done on the Element level.
复制代码



我只能用这个库才能跑起来
  1. from xml import etree
复制代码







小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-22 08:30:49 | 显示全部楼层
Nelson盟新 发表于 2020-3-21 22:49
用这个查的
https://github.com/python/cpython/tree/3.8/Lib/

查了一下,xml是python自带的库。
我用的lxml是第三方库,需要单独安装。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-14 20:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表