Skip to content

Instantly share code, notes, and snippets.

@NdYAG
Last active October 9, 2015 03:48
Show Gist options
  • Save NdYAG/3433526 to your computer and use it in GitHub Desktop.
Save NdYAG/3433526 to your computer and use it in GitHub Desktop.
Get Word Meaning and Sentences
#Search meanings and sentences of words from qq dict
import httplib,urllib,json
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def getJson(word):
url="http://dict.qq.com/dict?q="+word
url=url.encode('utf-8')
f=urllib.urlopen(url)
ret=f.read()
#return ret
if ret[2:7]=='local':
return ret
else:
return 'error'
def parseJson(jsonstr):
j=json.loads(jsonstr,"utf-8")
word=j["local"][0]["word"]
ret=word+"\t"
des=j["local"][0]["des"]
#definitions
for d in des:
ret+=d["d"]
ret+="\t"
#sentences
try:
sen=j["local"][0]["sen"]
for s in sen:
#print("{0}{1}".format(s["s"][0]["es"],s["s"][0]["cs"].encode('utf-8')))
ret+="{0}{1}".format(s["s"][0]["es"],s["s"][0]["cs"].encode('utf-8'))
except KeyError:
pass
else:
pass
return ret
def main():
f=open('word.txt')
fw=open('result.txt','a')
while True:
line=f.readline()
if len(line)==0:
break
else:
retjson=getJson(line)
if retjson!='error':
fw.write(parseJson(retjson)+"\n")
else:
print(line[0:-2]+" not found")
f.close()
fw.close()
print "Finished!"
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment