Hack Forums
[Python] Simple dictionary in python using urllib2 and dictionary.com - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Simple dictionary in python using urllib2 and dictionary.com (/showthread.php?tid=3816332)



Simple dictionary in python using urllib2 and dictionary.com - ImportUsername - Oct 20, 2013

Dictionary as in the thing that looks up what words mean, not "{}". Anyway Yea, turns out it's really nice and easy to write, also very short. The essential parts are only 5 lines long, but this one catches exceptions if the word isn't real, and also just looks a little nicer:
http://pastebin.com/2r8z3PZM
OR
Spoiler (Click to View)

And condensed to the minimum amount of lines* (for whatever reason)
Spoiler (Click to View)
*It could probably be less, but I'm not perfect and it also gets a lot harder to read

Anyway there you go. Never again will you have to wonder what a word means πŸ‘


* - wroteshea - Jan 6, 2014

Thanks alot, will learn from this! Stoked.


* - CodeX - Jan 6, 2014

A minor note: you should use urlencode on the word as it could be something like can't (can%27t) or Crème (Cr%C3%A8me)


* - KingofPython - Jan 6, 2014

Great! Learned a lot from this! I might look into remaking this my own way :)


* - ImportUsername - Jan 6, 2014

(Jan 6, 2014 07:52 AM)CodeX Wrote:  A minor note: you should use urlencode on the word as it could be something like can't (can%27t) or CrΓ¨me (Cr%C3%A8me)

You bring up a good point. I can't edit the original post, but here is the fix:

add this line:
Code
word = word[0].upper()+word[1:]
right after word is gotten from input

replace this line:
Code
page = urllib2.urlopen("http://dictionary.reference.com/browse/"+word+"?s=t").read()
with this line:
Code
page = urllib2.urlopen("http://dictionary.reference.com/browse/"+urllib.quote_plus(word)+"?s=t").read()

and replace this line:
Code
import urllib2
with this line:
Code
import urllib2,urllib

πŸ€—πŸ€—πŸ€—πŸ€—πŸ€—πŸ€— EDIT: 400th post hot dam! πŸ€—πŸ€—πŸ€—πŸ€—πŸ€—πŸ€—