• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > 使用Python从有道词典网页获取单词翻译

使用Python从有道词典网页获取单词翻译

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了python有道词典,python有道,python有道翻译,有道词典网页版,有道词典网页翻译等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

从有道词典网页获取某单词的中文解释。

import re
import urllib

word=raw_input('input a word\n')
 
url='http://dict.youdao.com/search?q=%s'%word
 
content=urllib.urlopen(url)
 
pattern=re.compile("</h2.*?</ul>",re.DOTALL)
 
result=pattern.search(content.read()).group()
pattern2=re.compile('<li>.*?</li>')
for i in pattern2.findall(result):
  print i.strip('<li>').strip('</li>').decode('utf-8')

</div>

再给大家分享一个命令行版的

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date  : 2014-04-03 21:12:16
# @Function: 有道翻译命令行版
# @Author : BeginMan

import os
import sys
import urllib
import urllib2
reload(sys)
sys.setdefaultencoding("utf-8")
import simplejson as json
import platform
import datetime

API_KEY = '******'
KEYFORM = '******'
  
def GetTranslate(txt):
  url = 'http://fanyi.youdao.com/openapi.do'
  data = {
  'keyfrom': KEYFORM,
  'key': API_KEY,
  'type': 'data',
  'doctype': 'json',
  'version': 1.1,
  'q': txt
  }
  data = urllib.urlencode(data)
  url = url+'?'+data
  req = urllib2.Request(url)
  response = urllib2.urlopen(req)
  result = json.loads(response.read())
  return result
  
def Sjson(json_data):
  query = json_data.get('query','')        # 查询的文本
  translation = json_data.get('translation','')  # 翻译
  basic = json_data.get('basic','')        # basic 列表
  sequence = json_data.get('web',[])       # 短语列表
  phonetic,explains_txt,seq_txt,log_word_explains = '','','',''
  
  # 更多释义
  if basic:
    phonetic = basic.get('phonetic','')     # 音标
    explains = basic.get('explains',[])     # 更多释义 列表
    for obj in explains:
      explains_txt += obj+'\n'
      log_word_explains += obj+','  
  # 句子解析
  if sequence:
    for obj in sequence:
      seq_txt += obj['key']+'\n'
      values = ''
      for i in obj['value']:
        values += i+','
      seq_txt += values+'\n'
    
  print_format = '*'*40+'\n'
  print_format += u'查询对象: %s [%s]\n' %(query,phonetic)  
  print_format += explains_txt
  print_format += '-'*20+'\n'+seq_txt
  print_format += '*'*40+'\n'
  print print_format
  choices = raw_input(u'是否写入单词本,回复(y/n):')
  if choices in ['y','Y']:
    filepath = r'/home/beginman/pyword/%s.xml' %datetime.date.today()
    if (platform.system()).lower() == 'windows':
      filepath = r'E:\pyword\%s.xml' %datetime.date.today()
    fp = open(filepath,'a+')
    file = fp.readlines()
    if not file:
      fp.write('<wordbook>\n')
      fp.write(u"""  <item>\n  <word>%s</word>\n  <trans><![CDATA[%s]]></trans>\n  <phonetic><![CDATA[[%s]]]></phonetic>\n  <tags>%s</tags>\n  <progress>1</progress>\n  </item>\n\n""" %(query,log_word_explains,phonetic,datetime.date.today()))
    fp.close()
    print u'写入成功.'

def main():
  while True:
    txt = raw_input(u'请输入要查询的文本:\n')
    if txt:
      Sjson(GetTranslate(txt))

if __name__ == '__main__':
  main()
</div>

以上就是本文的所有内容了,希望大家能够喜欢

</div>

您可能想查找下面的文章:

  • 使用Python从有道词典网页获取单词翻译
  • 使用Python从有道词典网页获取单词翻译
  • Python基于有道实现英汉字典功能

相关文章

  • Python去除字符串两端空格的方法
  • Python随机生成信用卡卡号的实现方法
  • python列表去重的二种方法
  • wxpython学习笔记(推荐查看)
  • 将图片文件嵌入到wxpython代码中的实现方法
  • Python编程中字符串和列表的基本知识讲解
  • Python监控主机是否存活并以邮件报警
  • python转换摩斯密码示例
  • python实现DES加密解密方法实例详解
  • python求crc32值的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 使用Python编写一个在Linux下实现截图分享的脚本的教程
    • Python中的random()方法的使用介绍
    • Python字符串和文件操作常用函数分析
    • python3.3使用tkinter开发猜数字游戏示例
    • python写日志封装类实例
    • Python脚本实现下载合并SAE日志
    • Python字符遍历的艺术
    • Python减少循环层次和缩进的技巧分析
    • Python 类的继承实例详解
    • Python 基础教程之包和类的用法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有