Last active
December 10, 2015 08:18
-
-
Save vladkorotnev/4406456 to your computer and use it in GitHub Desktop.
Get reading for a word
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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