使用Python從有道詞典網(wǎng)頁獲取單詞翻譯
更新時(shí)間:2016年07月03日 15:00:00 投稿:hebedich
這篇文章主要介紹了使用Python從有道詞典網(wǎng)頁獲取單詞翻譯的相關(guān)資料,需要的朋友可以參考下
從有道詞典網(wǎng)頁獲取某單詞的中文解釋。
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')
再給大家分享一個(gè)命令行版的
#!/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','') # 音標(biāo)
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'是否寫入單詞本,回復(fù)(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()
以上就是本文的所有內(nèi)容了,希望大家能夠喜歡
相關(guān)文章
Python基于回溯法子集樹模板解決取物搭配問題實(shí)例
這篇文章主要介紹了Python基于回溯法子集樹模板解決取物搭配問題,簡單描述了搭配問題并結(jié)合實(shí)例形式分析了Python使用回溯法子集樹模板解決取物搭配問題的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
Python連接MySQL數(shù)據(jù)庫的簡單便捷方法
在數(shù)據(jù)分析過程中往往要操作較大的數(shù)據(jù)集,這就需要連接數(shù)據(jù)庫進(jìn)行操作,下面這篇文章主要給大家介紹了關(guān)于Python連接MySQL數(shù)據(jù)庫的簡單便捷方法,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
使用OpenCV-python3實(shí)現(xiàn)滑動條更新圖像的Canny邊緣檢測功能
這篇文章主要介紹了使用OpenCV-python3實(shí)現(xiàn)滑動條更新圖像的Canny邊緣檢測功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
使用python找出list列表中相同元素(指定元素)的所有索引
這篇文章主要給大家介紹了關(guān)于使用python找出list列表中相同元素(指定元素)的所有索引,在平時(shí)開發(fā)過程中經(jīng)常遇到需要在數(shù)據(jù)中獲取特定的元素索引的信息,需要的朋友可以參考下2023-08-08
解決python字典對值(值為列表)賦值出現(xiàn)重復(fù)的問題
今天小編就為大家分享一篇解決python字典對值(值為列表)賦值出現(xiàn)重復(fù)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01

