从有道词典网页获取某单词的中文解释。
import re import urllib word=raw_input('input a word\n') url='http://dict.youdao.com/search"</h2.*",re.DOTALL) result=pattern.search(content.read()).group() pattern2=re.compile('<li>.*"htmlcode">#!/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+'""" <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()以上就是本文的所有内容了,希望大家能够喜欢