使用Python从有道词典网页获取单词翻译
从有道词典网页获取某单词的中文解释。
importre importurllib word=raw_input('inputaword\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>') foriinpattern2.findall(result): printi.strip('<li>').strip('</li>').decode('utf-8')
再给大家分享一个命令行版的
#!/usr/bin/envpython #-*-coding:utf-8-*- #@Date:2014-04-0321:12:16 #@Function:有道翻译命令行版 #@Author:BeginMan importos importsys importurllib importurllib2 reload(sys) sys.setdefaultencoding("utf-8") importsimplejsonasjson importplatform importdatetime API_KEY='******' KEYFORM='******' defGetTranslate(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()) returnresult defSjson(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='','','','' #更多释义 ifbasic: phonetic=basic.get('phonetic','')#音标 explains=basic.get('explains',[])#更多释义列表 forobjinexplains: explains_txt+=obj+'\n' log_word_explains+=obj+',' #句子解析 ifsequence: forobjinsequence: seq_txt+=obj['key']+'\n' values='' foriinobj['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' printprint_format choices=raw_input(u'是否写入单词本,回复(y/n):') ifchoicesin['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() ifnotfile: 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() printu'写入成功.' defmain(): whileTrue: txt=raw_input(u'请输入要查询的文本:\n') iftxt: Sjson(GetTranslate(txt)) if__name__=='__main__': main()
以上就是本文的所有内容了,希望大家能够喜欢