本帖最后由 949825667@qq.co 于 2016-8-30 16:45 编辑
| | | | | | | | | | | | | import os | | import sys | | import urllib | | import urllib2 | | reload(sys) | | sys.setdefaultencoding("utf-8") | | import simplejson as json | | import platform | | import datetime | | | | API_KEY = '1542783714' | | KEYFORM = 'dyzwordyoudao' | | | | | | 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','') | | 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' | | | | choices = raw_input(u'y or n') | | if choices in ['y','Y']: | | filepath = r'/home/beginman/pyword/%s.xml' %datetime.date.today() | | if (platform.system()).lower() == 'windows': | | filepath = r'C:\Python27\%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'success.' | | | | | | | | | | def main(): | | f = open('1.txt','r') | | while True: | | line = f.readline() | | if line: | | txt = line | | Sjson(GetTranslate(txt)) | | | | if __name__ == '__main__': | | main()COPY |
|