1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| __author__ = "orion-c"
import MySQLdb import urllib import urllib2 import json import xlwt
def connectMysql(): db = MySQLdb.connect("127","root","root","database" ) cursor = db.cursor() return cursor,db
def test_go(entry,name): url = "http://fanyi.baidu.com/v2transapi" values = { "from": "en", "to": "zh", "query": "%s"%name, "transtype": "realtime", "simple_means_flag": "3", } headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0', "Accept": "*/*", "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3", "Referer": "http://fanyi.baidu.com/?aldtype=16047", "Connection": "keep-alive", "X-Requested-With": "XMLHttpRequest", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Cookie":"BIDUPSID=124ADB9BC1148083F3FD928F0AB6E0D9; PSTM=1463040002; __cfduid=d3ce7d30fd22790d102423710107feb9c1464238819; BDUSS=mlKUU8tRTFOeWhnNXVmVDFmS01JZklWUnUwN09vUk9ObUhzRkxkTjVMYjJxdVZZSVFBQUFBJCQAAAAAAAAAAAEAAACj9i0bdDg4MDIxNnQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPYdvlj2Hb5Yf; BAIDUCUID=++; BAIDUID=0DBED8BF3B45F6955CA3FFDA47BDEF62:FG=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=1462_13551_21125_22746_17001_22917; PSINO=3; locale=zh; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1494913008; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1494913008; from_lang_often=%5B%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%2C%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%5D; to_lang_often=%5B%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%2C%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%5D; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1" } zhcnData = '' data = urllib.urlencode(values) req = urllib2.Request(url, data, headers) try: response = urllib2.urlopen(req) responsedata = response.read() hjosn = json.loads(responsedata) dst = hjosn['trans_result']['data'][0]['dst'] zhcnData = dst.encode('utf8') except: print ("this is error with entry :%s"%entry) return zhcnData
cursor,db = connectMysql()
sql = 'select entry,NAME from item_template WHERE entry > 70000 and entry < 80000' state = cursor.execute(sql)
xls = xlwt.Workbook() sheet = xls.add_sheet("Sheet1") row = 0
for data in cursor.fetchall(): entry = data[0] enNanme = data[1] cnName = test_go(entry,enNanme) if cnName != '': sheet.write(row, 0, entry) sheet.write(row, 1, enNanme) sheet.write(row, 2, cnName.decode('utf8')) row += 1 print row,entry,enNanme,cnName
xls.save('translate.xls')
|