由于有很多數據需要逐個翻譯,手工處理比較麻煩,花了十來分鐘寫了個python程序去跑,輕松了很多。具體代碼如下:
#encoding=utf-8
from __future__ import with_statement
import MySQLdb
import urllib
from lister import ListerTR
conn = MySQLdb.connect(host="localhost", user="root", passwd="root", db="coocoo", charset="utf8")
cursor = conn.cursor()
cursor.execute("select id, enname from compound where enname != '' and zhname = '' order by id")
row=cursor.fetchall()
for r in row:
params = urllib.urlencode({'eng2chi':r[1]}) 這里組織參數
sock = urllib.urlopen("http://202.127.145.134/scdb/translate/translate.asp", params) 發送請求,并把參數傳過去
html = sock.read()
sock.close()
p = ListerTR() 以下為解析返回的數據代碼
p.feed(html)
html = p.html
if u"成功" in html:
value = p.values[5]
data = value.strip()
print r[0], r[1], data 取回翻譯成功的內容更新數據庫里面的值
cursor.execute("update compound set zhname = %s where id=%s", (data, r[0]))
conn.commit()
else: 把翻譯失敗的記錄給文本文件中
with open('failture.txt', 'a+') as f:
f.write(str(r[0])+" | "+str(r[1]))
f.write('\n')
cursor.close()
conn.close()
posted on 2009-07-28 14:32
周銳 閱讀(3906)
評論(0) 編輯 收藏 所屬分類:
MySQL 、
Python