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

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

# 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. 【深度学习】(3) 全连接层、激活函数
  2. Traffic Ccontrol(流量控制)
  3. Python基础教程:list中的append()和extend()方法区别
  4. GAN生成对抗网络-PIX2PIXGAN原理与基本实现-图像翻译09
  5. 【svn】svn的使用
  6. python检查https过期_记录用certbot续签HTTPS时,python的pip源出现问题
  7. CoInitialize和CoInitializeEx
  8. python入门之运算符的使用的答案_python入门教程之基本算术运算符
  9. java map集合 事务控制_对象回收过程?线程池执行过程? map原理?集合类关系?synchronized 和 volatile ? 同一个类的方法事务传播控制还有作用吗?java 锁...
  10. 天哪,flash cs4可以使动态文本旋转了
  11. ubuntu mysql双主热备配置_MySql双主热备配置
  12. 面向对象——意图与逻辑(五)
  13. mybatis对mysql进行分页
  14. 如何获取项目中文件的路径
  15. ubuntu16.04 修改分辨路并永久保存
  16. FMEA学习(2软件)
  17. 戴尔服务器开机自动关机,戴尔台式电脑自动关机怎么办
  18. ubuntu 18.04快捷显示桌面
  19. 【有始有终,不让自己的付出辜负了自己期望 】
  20. 您选择的分区不支持无损调整容量操作

热门文章

  1. Git必知必会的操作
  2. Windows Server群集感知更新(CAU)-下
  3. optimus切换 linux,在Ubuntu上安装和配置Nvidia Optimus驱动
  4. 创建星宿与生日对照的数组
  5. linux控制台单人五子棋简书,案例:五子棋
  6. edger多组差异性分析_edgeR差异基因分析的一般过程
  7. 【LeetCode】【esay】【69】x的平方
  8. 字节跳动内部学习资料泄露!徐州java培训哪个好
  9. js遍历对象的属性并且动态添加属性
  10. 盈亏比例 php,()是指某一行业内企业的固定成本和可变成本之间的比例。A.财务报表结构B.盈亏平衡点...