Python查询五笔编码、拼音

可以查询五笔的网站很多,还是觉得不方便,本来想利用一个字典,实现本地查询,结果没找到。下面是一个用Python写的从http://qq.ip138.com/在线查询汉字五笔编码和拼音的程序。

#!/usr/bin/env python
#coding=utf-8
#author: Jianjun Kong
#website:http://kongove.whostas.com

import urllib,cookielib,urllib2,httplib
import sys,string,os

web_url = "http://qq.ip138.com/"
web_url += "wb/wb.asp?"
str=string.join(sys.argv[1:],'')

def init():
    cookie = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))

    exheaders = [("User-Agent","Firefox/3.05 (X11; Linux x86_64; U;en)"),("Connection","Keep-Alive"),("Referer","http://qq.ip138.com/"),("Accept","text/html,application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg,image/gif, image/x-xbitmap, */*;q=0.1"),("Accept-Charset","iso-8859-1,utf-8, utf-16, *;q=0.1"),("Cookie2","$Version=1"),]

    opener.addheaders = exheaders
    urllib2.install_opener(opener)
    return opener

def search(opener):
    global web_url,str

    body = (("querykey",str.decode('utf-8').encode('gb2312')),)
    ret  = opener.open(web_url,urllib.urlencode(body))
    str=ret.read()
    str=str.decode('gb2312').encode('utf-8')
    f=open("/tmp/wubi.tmp","w")
    f.write("%s" %(str))
    f.close()

def main(argv=None):
    opener=init()
    search(opener)

    cmds='''
sed -i -e '1,/<form name="form1" method="post"/d' /tmp/wubi.tmp;
sed -i -e '1,//d' /tmp/wubi.tmp;
sed -i -e '/
/,$d' /tmp/wubi.tmp; sed -i -e 's//rn/g' /tmp/wubi.tmp; sed -i -e '1,/汉语拼音/d' /tmp/wubi.tmp; sed -i -e 's/、/t/g' /tmp/wubi.tmp; sed -i -e 's//t/g' /tmp/wubi.tmp; sed -i -e 's/]*>//g;/<!--/d' /tmp/wubi.tmp; sed -i -e 's/ //g;s/°C//g;s/^s*//g;/^$/d' /tmp/wubi.tmp ''' cmd="sed -i -e '1,/<form name="form1" method="post"/d' /tmp/wubi.tmp; sed -i -e '1,//d' /tmp/wubi.tmp; sed -i -e '/
/,$d' /tmp/wubi.tmp; sed -i -e 's//\r\n/g' /tmp/wubi.tmp; sed -i -e '1,/汉语拼音/d' /tmp/wubi.tmp; sed -i -e 's/、/\t/g' /tmp/wubi.tmp; sed -i -e 's//\t/g' /tmp/wubi.tmp; sed -i -e 's/]*>//g;/<!--/d' /tmp/wubi.tmp; sed -i -e 's/ //g;s/°C//g;s/^s*//g;/^$/d' /tmp/wubi.tmp" os.system(cmd) print "序号t汉字t五笔t拼音" #exit() f=open("/tmp/wubi.tmp","r") print f.read(), f.close() if __name__ == "__main__": sys.exit(main())

运行效果

kongove@ubuntu:~/code/python/wubi$ ./wubi.py 汉字 五笔拼音 查询
序号	汉字	五笔	拼音
1	汉	icy	hàn
2	字	pbf	zì
3	五	gghg	wǔ
4	笔	ttfn	bǐ
5	拼	ruah	pīn
6	音	ujf	yīn
7	查	sjgf	chá
8	询	yqjg	xún
kongove@ubuntu:~/code/python/wubi$ 

2 thoughts on “Python查询五笔编码、拼音

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.