rendered paste body# import re
import urllib
import sys
import getopt
from lxml import etree, html
# from util import hook
# @hook.command('u')
# @hook.command
def zdic(inp):
'''.zd/.zdic <Chinese word in characters> -- looks up <word> on zdic.net/cd/'''
url = 'http://www.zdic.net/sousuo'
params = {'lb_a': 'hp', 'lb_b': 'mh', 'lb_c': 'mh', '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))
import pdb; pdb.set_trace()
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()