posts - 431,  comments - 344,  trackbacks - 0

          由于有很多數(shù)據(jù)需要逐個翻譯,手工處理比較麻煩,花了十來分鐘寫了個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]})      這里組織參數(shù)
              sock = urllib.urlopen("http://202.127.145.134/scdb/translate/translate.asp", params)   發(fā)送請求,并把參數(shù)傳過去
              html = sock.read()
              sock.close()
              p = ListerTR()   以下為解析返回的數(shù)據(jù)代碼
              p.feed(html)
              html = p.html
              if u"成功" in html:
                  value = p.values[5]
                  data = value.strip()
                  print r[0], r[1], data    取回翻譯成功的內(nèi)容更新數(shù)據(jù)庫里面的值
                  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 周銳 閱讀(3910) 評論(0)  編輯  收藏 所屬分類: MySQLPython
          主站蜘蛛池模板: 贡嘎县| 普格县| 赤壁市| 两当县| 千阳县| 汉中市| 奉新县| 泰来县| 汽车| 南江县| 靖宇县| 鄢陵县| 盐边县| 华亭县| 青河县| 克拉玛依市| 千阳县| 措美县| 将乐县| 马关县| 化隆| 和硕县| 杂多县| 乳源| 从江县| 沾益县| 菏泽市| 图们市| 赤城县| 日喀则市| 射洪县| 肥乡县| 修文县| 吉安县| 子洲县| 阳原县| 长兴县| 安岳县| 林芝县| 长阳| 山西省|