Skip to content

Instantly share code, notes, and snippets.

@chew-z
Created March 2, 2015 16:13
Show Gist options
  • Save chew-z/5c37b6f87e8262d3557d to your computer and use it in GitHub Desktop.
Save chew-z/5c37b6f87e8262d3557d to your computer and use it in GitHub Desktop.
#coding: utf-8
#translates using Google Translate via web
import requests
import re
import clipboard
from bs4 import BeautifulSoup
def translate(phrase):
if phrase == '':
raise Exception('Nothing found in clipboard')
userdata = {'q': phrase,'sl': 'fr' , 'tl': 'pl'}
response = requests.post("https://translate.google.com/", params=userdata)
print response.url
#print response.headers
return response.text
text = clipboard.get()
html = translate(text)
parsed_html = BeautifulSoup(html)
print parsed_html.body.find('div', attrs={'id':'gt-res-content'}).text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment