Skip to content

Instantly share code, notes, and snippets.

@wynemo
Created January 21, 2012 05:39
Show Gist options
  • Save wynemo/1651533 to your computer and use it in GitHub Desktop.
Save wynemo/1651533 to your computer and use it in GitHub Desktop.
grasp word definition from google
#!/usr/bin/env python
#coding:utf-8
import re
import sys
import urllib2
std_headers = {
'User-Agent': 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.12) Gecko/20101028 Firefox/3.6.12',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-us,en;q=0.5',
}
def main():
url1='http://www.google.com/search?hl=en&tbs=dfn:1&q='
request = urllib2.Request(url1+sys.argv[1], None, std_headers)
i1 = urllib2.urlopen(request)
str1=i1.read()
pattern1=r'<\s*?div\s+?id\s*?=\s*?"\s*?ires\s*?".+<\s*?/\s*?ol\s*?>\s*?<\s*?/div\s*?>'
pattern2=r'<\s*?h5.+?>\s*?Web\s+definitions.+?(?=</ol>)'
o1=re.search(pattern1,str1,re.S|re.I|re.X)
if o1:
g1=o1.group()
rt_str = re.sub(pattern2,'',g1,1,re.S|re.I|re.X)
print rt_str
i1.close()
if __name__=="__main__":
main()
#mk_dict.py tutor > tutor.htm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment