用的是Python,虽然Bash也可以。初学者一个……欢迎各种指教
需要安装一些额外的东西
代码: 全选
sudo aptitude install xsel libnotify-bin
可以用命令行。或者绑定快捷键(比如win+Q),选中要查询的文字,按快捷键就好了
--------
改了一下……dict.cn找不到是也用google
代码: 全选
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#Author: Roy L Zuo (roylzuo at gmail dot come)
#Last Change: Sat Sep 27 07:30:22 2008 EST
#Description: various actions to perform on selected text
#修改: lainme([email protected]) 2010年09月03日
import re, sys, os
import urllib, urllib2, socket
notifyargs = "notify-send -t 5000 -i gtk-dialog-info"
socket.setdefaulttimeout(10)
string = (" ".join(sys.argv[1:]) if len(sys.argv)>=2 else (os.popen('xsel -o').read().strip()))
if string=="":
os.system("%s '你必须给定一个参数'" %(notifyargs))
elif re.match("^(\d{1,3}\.){3}\d{1,3}$", string):
#ip查询
data = urllib.urlencode({"action": 2, "ip": string})
url = "http://www.ip138.com/ips.asp?"
try:
outstr = urllib2.urlopen(url, data).read().decode("gb2312").encode("utf8")
result = re.search('本站主数据:(.*?)</li', outstr).group(1)
os.system("%s '%s地址在:' '%s'" %(notifyargs, string, result))
except:
os.system("%s '获取数据出错'" %(notifyargs))
else:
#查单词
dict_data = urllib.urlencode({"utf8": "true", "q": string})
dict_url = "http://api.dict.cn/ws.php?"
g_data = urllib.urlencode({"langpair": "en|zh-CN", "v": "1.0", "q": string})
g_url = "http://ajax.googleapis.com/ajax/services/language/translate?"
try:
outstr = urllib2.urlopen(dict_url, dict_data).read()
result = re.search("<def>([^<]*)", outstr).group(1)
if result == "Not Found":
raise exception
os.system("%s '%s的释义(Dict.CN):' '%s'" %(notifyargs, string, result))
except:
try:
outstr = urllib2.urlopen(g_url, g_data).read()
result = re.search("{\"translatedText\":\"([^\"]*)\"}", outstr).group(1)
os.system("%s '%s的释义(Google):' '%s'" %(notifyargs, string, result))
except:
os.system("%s '获取数据出错'" %(notifyargs))