欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年07月03日 15:00:00   投稿:hebedich  
這篇文章主要介紹了使用Python從有道詞典網(wǎng)頁獲取單詞翻譯的相關(guān)資料,需要的朋友可以參考下

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

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("</h2.*?</ul>",re.DOTALL)
 
result=pattern.search(content.read()).group()
pattern2=re.compile('<li>.*?</li>')
for i in pattern2.findall(result):
  print i.strip('<li>').strip('</li>').decode('utf-8')

再給大家分享一個命令行版的

#!/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',[])       # 短語列表
  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'查詢對象: %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'是否寫入單詞本,回復(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('<wordbook>\n')
      fp.write(u"""  <item>\n  <word>%s</word>\n  <trans><![CDATA[%s]]></trans>\n  <phonetic><![CDATA[[%s]]]></phonetic>\n  <tags>%s</tags>\n  <progress>1</progress>\n  </item>\n\n""" %(query,log_word_explains,phonetic,datetime.date.today()))
    fp.close()
    print u'寫入成功.'

def main():
  while True:
    txt = raw_input(u'請輸入要查詢的文本:\n')
    if txt:
      Sjson(GetTranslate(txt))

if __name__ == '__main__':
  main()

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

相關(guān)文章

  • python獲取文件版本信息、公司名和產(chǎn)品名的方法

    python獲取文件版本信息、公司名和產(chǎn)品名的方法

    這篇文章主要介紹了python獲取文件版本信息、公司名和產(chǎn)品名的方法,是Python程序設(shè)計中非常實用的技巧,需要的朋友可以參考下
    2014-10-10
  • Python編程實現(xiàn)兩個文件夾里文件的對比功能示例【包含內(nèi)容的對比】

    Python編程實現(xiàn)兩個文件夾里文件的對比功能示例【包含內(nèi)容的對比】

    這篇文章主要介紹了Python編程實現(xiàn)兩個文件夾里文件的對比功能,包含內(nèi)容的對比操作,涉及Python文件與目錄的遍歷、比較、運算等相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06
  • python實現(xiàn)多進程通信實例分析

    python實現(xiàn)多進程通信實例分析

    這篇文章主要介紹了python實現(xiàn)多進程通信實例分析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Pytest執(zhí)行unittest TestSuite(測試套件)的實現(xiàn)方法

    Pytest執(zhí)行unittest TestSuite(測試套件)的實現(xiàn)方法

    TestSuite一直是unittest的靈活與精髓之處,在繁多的測試用例中,可以任意挑選和組合各種用例集,這篇文章主要介紹了Pytest執(zhí)行unittest TestSuite(測試套件)的實現(xiàn)方法,需要的朋友可以參考下
    2021-08-08
  • 實例講解Python中整數(shù)的最大值輸出

    實例講解Python中整數(shù)的最大值輸出

    在本篇文章里小編給大家分享了關(guān)于Python中整數(shù)的最大值輸出的實例內(nèi)容,以及相關(guān)知識點,需要的朋友們學(xué)習(xí)下。
    2019-03-03
  • python用tkinter實現(xiàn)一個gui的翻譯工具

    python用tkinter實現(xiàn)一個gui的翻譯工具

    這篇文章主要介紹了python用tkinter實現(xiàn)一個gui的翻譯工具,幫助大家更好的理解和使用python,感興趣的朋友可以了解下 +
    2020-10-10
  • Python?操作Excel-openpyxl模塊用法實例

    Python?操作Excel-openpyxl模塊用法實例

    openpyxl 模塊是一個讀寫 Excel 2010 文檔的 Python 庫,如果要處理更早格式的 Excel 文 檔,需要用到額外的庫,openpyxl 是一個比較綜合的工具,能夠同時讀取和修改 Excel 文檔,這篇文章主要介紹了Python?操作Excel-openpyxl模塊使用,需要的朋友可以參考下
    2023-05-05
  • Python解惑之整數(shù)比較詳解

    Python解惑之整數(shù)比較詳解

    這篇文章主要給大家介紹了Python中整數(shù)比較的相關(guān)資料,文中通過示例代碼介紹的非常詳細,詳細會對大家學(xué)習(xí)python的整數(shù)具有一定的參考價值,需要的朋友下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-04-04
  • Python實現(xiàn)隨機游走的示例代碼

    Python實現(xiàn)隨機游走的示例代碼

    隨機游走是一個數(shù)學(xué)對象,稱為隨機或隨機過程,它描述了一條路徑,該路徑由一些數(shù)學(xué)空間上的一系列隨機步驟組成,下面我們就來學(xué)習(xí)一下Python如何實現(xiàn)隨機游走的吧
    2023-12-12
  • Python?Httpx庫實現(xiàn)超跑式網(wǎng)絡(luò)請求用法實例

    Python?Httpx庫實現(xiàn)超跑式網(wǎng)絡(luò)請求用法實例

    這篇文章主要為大家介紹了Python?Httpx庫實現(xiàn)超跑式網(wǎng)絡(luò)請求用法實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01

最新評論