日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

使用Python從有道詞典網(wǎng)頁(yè)獲取單詞翻譯

系統(tǒng) 1759 0

從有道詞典網(wǎng)頁(yè)獲取某單詞的中文解釋。

            
import re
import urllib

word=raw_input('input a word\n')
 
url='http://dict.youdao.com/search?q=%s'%word
 
content=urllib.urlopen(url)
 
pattern=re.compile("",re.DOTALL)
 
result=pattern.search(content.read()).group()
pattern2=re.compile('
            
  • .*?
  • ') for i in pattern2.findall(result): print i.strip('
  • ').strip('
  • ').decode('utf-8')

    再給大家分享一個(gè)命令行版的

                
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Date  : 2014-04-03 21:12:16
    # @Function: 有道翻譯命令行版
    # @Author : BeginMan
    
    import os
    import sys
    import urllib
    import urllib2
    reload(sys)
    sys.setdefaultencoding("utf-8")
    import simplejson as json
    import platform
    import datetime
    
    API_KEY = '******'
    KEYFORM = '******'
      
    def GetTranslate(txt):
      url = 'http://fanyi.youdao.com/openapi.do'
      data = {
      'keyfrom': KEYFORM,
      'key': API_KEY,
      'type': 'data',
      'doctype': 'json',
      'version': 1.1,
      'q': txt
      }
      data = urllib.urlencode(data)
      url = url+'?'+data
      req = urllib2.Request(url)
      response = urllib2.urlopen(req)
      result = json.loads(response.read())
      return result
      
    def Sjson(json_data):
      query = json_data.get('query','')        # 查詢的文本
      translation = json_data.get('translation','')  # 翻譯
      basic = json_data.get('basic','')        # basic 列表
      sequence = json_data.get('web',[])       # 短語(yǔ)列表
      phonetic,explains_txt,seq_txt,log_word_explains = '','','',''
      
      # 更多釋義
      if basic:
        phonetic = basic.get('phonetic','')     # 音標(biāo)
        explains = basic.get('explains',[])     # 更多釋義 列表
        for obj in explains:
          explains_txt += obj+'\n'
          log_word_explains += obj+','  
      # 句子解析
      if sequence:
        for obj in sequence:
          seq_txt += obj['key']+'\n'
          values = ''
          for i in obj['value']:
            values += i+','
          seq_txt += values+'\n'
        
      print_format = '*'*40+'\n'
      print_format += u'查詢對(duì)象: %s [%s]\n' %(query,phonetic)  
      print_format += explains_txt
      print_format += '-'*20+'\n'+seq_txt
      print_format += '*'*40+'\n'
      print print_format
      choices = raw_input(u'是否寫(xiě)入單詞本,回復(fù)(y/n):')
      if choices in ['y','Y']:
        filepath = r'/home/beginman/pyword/%s.xml' %datetime.date.today()
        if (platform.system()).lower() == 'windows':
          filepath = r'E:\pyword\%s.xml' %datetime.date.today()
        fp = open(filepath,'a+')
        file = fp.readlines()
        if not file:
          fp.write('
                
                  \n')
          fp.write(u"""  
                  
                    \n  
                    
                      %s
                    
                    \n  
                    
                                      
                    \n  
                    
                                      
                    \n  
                    
                      %s
                    
                    \n  
                    
                      1
                    
                    \n  
                  
                  \n\n""" %(query,log_word_explains,phonetic,datetime.date.today()))
        fp.close()
        print u'寫(xiě)入成功.'
    
    def main():
      while True:
        txt = raw_input(u'請(qǐng)輸入要查詢的文本:\n')
        if txt:
          Sjson(GetTranslate(txt))
    
    if __name__ == '__main__':
      main()
                
              

    以上就是本文的所有內(nèi)容了,希望大家能夠喜歡


    更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

    微信掃碼或搜索:z360901061

    微信掃一掃加我為好友

    QQ號(hào)聯(lián)系: 360901061

    您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

    【本文對(duì)您有幫助就好】

    您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

    發(fā)表我的評(píng)論
    最新評(píng)論 總共0條評(píng)論
    主站蜘蛛池模板: 精河县| 金昌市| 泰宁县| 浮梁县| 平塘县| 万州区| 河西区| 定西市| 高唐县| 社会| 田东县| 阳江市| 崇明县| 桃源县| 邻水| 乌海市| 旺苍县| 台江县| 军事| 临夏市| 库伦旗| 柯坪县| 通河县| 台南县| 廊坊市| 宜宾县| 永城市| 富蕴县| 寻乌县| 六枝特区| 长武县| 视频| 保山市| 望谟县| 东山县| 吉林市| 黄梅县| 山丹县| 稻城县| 峨山| 乌什县|