Skip to content

Instantly share code, notes, and snippets.

@vladkorotnev
Last active December 10, 2015 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vladkorotnev/4406456 to your computer and use it in GitHub Desktop.
Save vladkorotnev/4406456 to your computer and use it in GitHub Desktop.
Get reading for a word
#!/usr/bin/env python
# coding=utf8
# do not touch the above or it will freak out
try:
from BeautifulSoup import BeautifulSoup
except:
print "REQUIRES BEAUTIFULSOUP!"
import urllib
import urllib2
import random
import sys
data = {'sl':'ja','tl':'en','text':sys.argv[1]}
querystring = urllib.urlencode(data)
request = urllib2.Request('http://www.translate.google.com' + '?' + querystring )
request.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11')
opener = urllib2.build_opener()
feeddata = opener.open(request).read()
soup = BeautifulSoup(feeddata)
reading = soup.find('div', id="src-translit").contents[0]
print "---- Original: "
print sys.argv[1]
print "---- Reading: "
print reading
print "---- Translation:"
print soup.find('span', id="result_box").contents[0].string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment