看着学吧:我就不多解释了,主要看注释就差不多了。

至于模块安装有疑问的话,可以留言在这里,我会尽力解答。

# author: rexora
# date: Sept 25 2014
import httplib2
from urllib.parse import quote
from bs4 import BeautifulSoup
from sys import argv
import sys
import timeclass Timer:def __enter__(self):self.start = time.time()def __exit__(self, *args): time_len = time.time() - self.startif time_len != 0:print('\n * * * 查询用了{:.2f}秒. * * * '.format( time_len + 0.8))# 加0.8秒是因为我认为程序进入需要时间,这样可以让timer显得更准确。    def youdao_en2ch(word):url = 'http://dict.youdao.com/search?q={}&keyfrom=dict.index'.format(word)_, resp = httplib2.Http("g:/.cache").request(url)content = resp.decode('utf-8')soup = BeautifulSoup(content)div_trans = soup.select('.trans-container') # if it's type, it'll only have less than 10 containers.result = []if len(div_trans) > 6:div = div_trans[0]for li in div.find_all('li'):print(li.string)  result.append(str(li.string))return (1, word, result) # 用1表示这时来自en2ch,2表示ch2en, this is a query containing 3 itemselse:try:typo = soup.select('.error-typo')[0]right_spell = typo.find('a').stringprint('你要找的是不是"{}" ?'.format(right_spell))            except IndexError:print('提示: 请仔细检查你的拼写,我没有找到这个词。')def youdao_ch2en(word):url = 'http://dict.youdao.com/search?le=eng&q={}&keyfrom=dict.top'.format(quote(word))_, resp = httplib2.Http("g:/.cache").request(url)content = resp.decode('utf-8')soup = BeautifulSoup(content)div_trans = soup.select('.trans-container')if len(div_trans) > 3:result = []div = div_trans[0]for branch in div.select('.wordGroup'): part_of_speech = branch.find('span') if part_of_speech.string != None:          print(part_of_speech.string, end = ' ') for meaning in branch.find_all('a'):print(meaning.string, end = '; ')result.append(str(part_of_speech.string)+' '+'; '.join([str(i.string) for i in branch.find_all('a')]))    print()    return(2, word, result)else:print('提示: 请仔细检查你输入的内容,我没有找到这个词。')def steal(query):if query != None:word = query[1]result = ' '.join(query[2])if query[0] == 1:with open('g:/yod/yod-en2ch.txt','a',encoding='utf-8') as f:print(word+'***'+result, file = f)if query[0] == 2:with open('g:/yod/yod-ch2en.txt','a',encoding='utf-8') as f:print(word+'***'+result,file = f)def input_detect():word = ' '.join(argv[1:])if word == '':return # 如果word是空,则直接退出这个函数,然后进入while True里面的循环。test = ''.join(argv[1:]).strip('\'') is_eng = Trueif quote(test) != test:is_eng = Falseif is_eng == True:steal(youdao_en2ch(word))else:steal(youdao_ch2en(word))if __name__ == '__main__':with Timer():   print()input_detect()while True:word = input('\n>>>')with Timer():argv = ['_', word]print()input_detect()

命令行实现英汉汉英词典相关推荐

  1. Python 英汉-汉英 词典脚本,可以安装到Linux命令中

    声明:脚本文件为原作者所有,我只是做了些自定义修改而已,脚本使用有道词典网页版作为查询API 将脚本扔到/usr/bin/下面,使用命令yd + 单词进行查询. 支持英汉查询和汉英查询. #! /us ...

  2. Windows/Linux 命令行下英汉互译软件

    一.dict 1.安装好 python 环境(windows 用户执行 python.exe 安装程序即可) sudo pip install dict-cli 2.安装 pip 工具(windows ...

  3. docmd怎么读_欧路词典|英汉-汉英词典 access是什么意思_access的中文解释和发音_access的翻译_access怎么读...

    Access to this information is severely restricted. 这一信息的使用受到极严格的限制. Delf Keylogger.This malicous pro ...

  4. friendly发音_欧路词典|英汉-汉英词典 friendly是什么意思_friendly的中文解释和发音_friendly的翻译_friendly怎么读...

    The refusal was couched in friendly language. 他用友善的语言表达了拒绝之意. He's a friendly chatty sort of person. ...

  5. liner中文翻译_欧路词典|英汉-汉英词典 liner是什么意思_liner的中文解释和发音_liner的翻译_liner怎么读...

    The transatlantic liner forged ahead through the waves. 横渡大西洋的客轮破浪前进. They sailed to America in a la ...

  6. 计算机专业词典_好书优选:实用英汉汉英计算机词典

    购买入口: 基本信息 书名:实用英汉汉英计算机词典 价格:98.00元 作者:王能琴,谢建勋 主编 出版社:北京航空航天大学出版社 出版日期:2009-03-01 ISBN:9787811243116 ...

  7. Python控制台英汉-汉英电子词典

    2019独角兽企业重金招聘Python工程师标准>>> Python控制台英汉-汉英电子词典 By qianghaohao(CodeNutter) 用python实现了下控制台电子词 ...

  8. Python控制台英汉-汉英电子词典(代码分享)

    Python控制台英汉-汉英电子词典 By qianghaohao(CodeNutter) 用python实现了下控制台电子词典,数据来自有道,通过向有道发送http表单数据, 然后将服务器返回的js ...

  9. c语言词典课程设计报告,英汉电子词典C语言课程设计

    英汉电子词典C语言课程设计 C 语言程序设计 课程设计(论文)题目: 英汉电子词典 院(系): 专业班级: 学 号: 学生姓名: 指导教师: 教师职称: 起止时间: 课程设计(报告)任务及评语院(系) ...

最新文章

  1. 正则化技巧:标签平滑(Label Smoothing)以及在 PyTorch 中的实现
  2. php点击字切换验证码,PHP生成图片验证码、点击切换实例 Web程序 - 贪吃蛇学院-专业IT技术平台...
  3. python 矩阵常用计算
  4. Java使用strategy模式构造程序
  5. Spark1.4 和 Hive 1.1.1 启动错误
  6. ActiveMQ在windows下启动失败解决方案
  7. SpringMVC核心——视图渲染(包含视图解析)问题
  8. 95-136-070-源码-Operator-扩展有状态的operators
  9. arcore与unity_Android ARCore –与相机的距离
  10. s3c2440中蜂鸣器的打开与关闭
  11. matlab状态反馈控制器设计,状态反馈控制器设计方案书.doc
  12. 微信公众号查后端服务器IP,微信公众号平台接口开发 获取微信服务器IP地址方法解析...
  13. WLAN和WIFI区别以及组网方式
  14. SQL人民币小写金额转大写
  15. 【阅读笔记】Deep Mutual Learning
  16. 不能撑开盒子css,CSS padding在什么情况下不撑开盒子
  17. 虚拟机、容器和沙箱是什么关系?
  18. C#时间格式转换问题(12小时制和24小时制)
  19. 2022-01-21 界面长亮不熄屏
  20. 打印机显示rpc服务器,大师讲解win10提示打印机rpc服务器不可用的处理办法

热门文章

  1. C++ cin输入空格
  2. ScreenFlow for Mac v10 中文版 优秀的屏幕录像软件
  3. 最新推荐:【凯立德 移动导航系统】【V4.0 破解版】3D高清地图
  4. Win10将用户名修改为英文亲测可用
  5. unity3d加载网上图片
  6. ROS:新手使用VScode过程中用launch文件进行多节点运行时遇到ERROR: cannot launch node of type
  7. 【Unity】Unity中开场动画设置
  8. 福禄克网线测线仪DSX2-5000中的光纤测试方法
  9. 春季水彩花卉花环花朵叶子剪贴画PNG免抠图
  10. WSL---Windows Subsystem for Linux