This article mainly introduces the python Automatic translation implementation method, which involves the skills related to Python's call to Baidu interfaces and database operations. if you need it, refer to the example below to describe how to implement python automatic translation. We will share this with you for your reference. The details are as follows:
I have learned the basics of python before and have never used it. Later, a parameter table must be in Chinese and English. Think about it, but use python. The Baidu translation interface has encountered garbled code, incomplete modules, and other problems. Generally, google does this. Share it.
# Encoding = UTF-8 # eagle_91@sina.com # created 2014-07-22import urllibimport urllib2import MySQLdbimport jsonimport gcimport timeurl =' http://openapi.baidu.com/public/2.0/bmt/translate '_ SleepTime = 0.5 _ limit = 1000 # link mysqlconn = MySQLdb. connect (host = 'localhost', user = 'root', passwd = '', charset = 'utf8') curs = conn. cursor () conn. select_db ('test') # search the count‑curs.exe cute ("" SELECT * FROM sb_parameters where isnull (en_name) order by id ASC "") # print curs. fetchall () # print countresults = curs. fetchmit (_ limit) for r in results: gc. collect () chin = unicode (r [3]). encode ('utf-8') # print chin values = {'Client _ id': 'pwrgllvvaifcd0syqaipwkav', 'Q': chin, 'from': 'zh ', 'to': 'en'} data = urllib. urlencode (values) req = urllib2.Request (url, data) response = urllib2.urlopen (req) the_page = response. read () returnData = json. loads (the_page) # print returnData if returnData. has_key ("error_code"): continue; inputData = returnData ["trans_result"] [0] ["dst"] try: SQL = "UPDATE 'sb _ parameters 'set 'en _ name' = '% s' WHERE 'id' = % d" % (MySQLdb. escape_string (unicode (inputData ). encode ('utf-8'), r [0]) print SQL curs.exe cute (SQL) conn. commit () time. sleep (_ sleepTime) failed t EOFError: SQL = "UPDATE 'sb _ parameters 'set 'en _ name' = '% s' WHERE 'id' = % d" % ('', r [0]) print SQL curs.exe cute (SQL) conn. commit () continueconn. commit () # Close the link curs. close () # close database conn. close ()
For more articles about python auto-translation implementation, refer to PHP Chinese network!