有时间再做总结!
# coding=utf-8 #authority:bing #2017-7-18 import httplib import md5 import urllib import urllib2 import random import sys reload(sys) sys.setdefaultencoding('utf8') # def transf(q): appid = '20151113000005349' secretKey = 'osubCEzlGjzvw8qdQc41' httpClient = None myurl = '/api/trans/vip/translate' # q = f.read(20000)#最多读这么多,再多点就会出问题。 #q = 'apple' fromLang = 'en' toLang = 'zh' salt = random.randint(32768, 65536) sign = appid + q + str(salt) + secretKey m1 = md5.new() m1.update(sign) sign = m1.hexdigest() myurl = myurl + '?appid=' + appid + '&q=' + urllib.quote(q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(salt) + '&sign=' + sign try: httpClient = httplib.HTTPConnection('api.fanyi.baidu.com') httpClient.request('GET', myurl) # response是HTTPResponse对象 response = httpClient.getresponse() # print response.read() cc = response.read() ccc = cc.decode("unicode_escape") ccc = ccc.encode("utf-8") file_object = open('thefile5.5.txt', 'w') file_object.write(ccc) file_object.close() except Exception, e: print e finally: if httpClient: httpClient.close() # transf('apple') f = open('sp/5.5.csv') transf(f.read()) print f.tell() #print f.readline() f.close() #print line.strip()