NOTE Python code is in section 1, javascript query from the search site is in section 2 SECTION 1 --------- # import re import urllib, urllib2 import sys import getopt from lxml import etree, html # from util import hook # @hook.command('u') # @hook.command def zdic(inp): '''.zd/.zdic -- looks up on zdic.net/cd/''' url = 'http://www.zdic.net/sousuo' # url = 'http://www.nciku.com/submitFeedback' params = {'lb_a': 'hp', 'lb_b': 'jq', 'lb_c': 'jq', 'tp': 'tp1', 'q': inp} # params = {'lb_a': 'hp', 'lb_b': 'jq', 'lb_c': 'jq', 'tp': 'tp1', 'q': inp} page = post_get(url, params) # page = http.get_html(url, term=inp) # words = page.xpath("//td[@class='word']") # defs = page.xpath("//div[@class='definition']") defs = page.xpath("//p[@class='zdct1']") pinyin = page.xpath("//span[@class='dicpy']") if not defs: return 'no definitions found' # out = defs[1].text_content().strip() + ': ' + ' '.join( # defs[0].text_content().split()) out = [None]*2 out[0] = defs[1].text_content().strip() + defs[2].text_content().strip() out[1] = defs[3].text_content().strip() + defs[4].text_content().strip() for c in out: if len(c) > 400: c = c[:c.rfind(' ', 0, 400)] + '...' return out def post_get(url, params): # f = urllib.urlopen(url, urllib.urlencode(params)) # user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0' headers = { 'User-Agent' : user_agent, 'Host' : 'www.zdic.net', 'Referer' : 'http://www.zdic.net/cd/' } data = urllib.urlencode(params) req = urllib2.Request(url, data, headers) import pdb; pdb.set_trace() f = urllib2.urlopen(req) text = f.read() outfile(text + '\n\n' + params['q']) return html.fromstring(text) def main(): # parse command line options try: opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) except getopt.error, msg: print msg print "for help use --help" sys.exit(2) # process options for o, a in opts: if o in ("-h", "--help"): print __doc__ sys.exit(0) # process arguments for arg in args: print zdic(arg) # process() is defined elsewhere def outfile(text): of = open('zdret.txt', 'w') of.write(text) of.close() if __name__ == "__main__": main() SECTION 2 --------- RELEVANT CONTENT OF /CD/ (SEARCH PAGE) CONTENT OF /JS/CD_SEC.JS CONTENT OF /JS/SECPAN.JS document.writeln("
"); document.writeln("
"); document.writeln("条目
|
"); document.writeln("字典
|
"); document.writeln("词典
|
"); document.writeln("成语
|
"); document.writeln("全站"); document.writeln("
"); document.writeln("
"); document.writeln("
请直接输入汉字或词语进行查询,支持拼音查询,例:“han”;“han4”;“han yu”;“han4 yu3”。
"); document.writeln("
"); document.writeln(" "); document.writeln(" "); document.writeln(" "); document.writeln(" "); document.writeln(" "); document.writeln(" "); document.writeln("
"); document.writeln("
"); document.writeln(" "); document.writeln(" "); document.writeln("
"); document.writeln("
"); document.writeln(" "); document.writeln(" "); document.writeln("
"); document.writeln(" "); document.writeln("
"); document.writeln("
"); document.writeln(" "); document.writeln("
"); document.writeln(" "); document.writeln("
"); document.writeln("
"); document.writeln("
"); document.writeln("
"); document.writeln(" "); document.writeln(" "); document.writeln("
"); document.writeln("
"); document.writeln(" "); document.writeln("
"); document.writeln(" "); document.writeln("
"); document.writeln("
"); document.writeln(" "); //document.writeln("
模糊搜索请加问号:中? | 旧版搜索框
"); document.writeln(""); document.writeln(" "); document.writeln(" "); document.writeln(" "); document.writeln("
"); document.writeln(" "); document.writeln("
");