Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3304082
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:01:28+00:00 2026-05-17T21:01:28+00:00

My python script is working now, but I’m having a little trouble: Here is

  • 0

My python script is working now, but I’m having a little trouble:

Here is the output:

from BeautifulSoup import BeautifulSoup
import urllib

langCode={
    "arabic":"ar", "bulgarian":"bg", "chinese":"zh-CN",
    "croatian":"hr", "czech":"cs", "danish":"da", "dutch":"nl",
    "english":"en", "finnish":"fi", "french":"fr", "german":"de",
    "greek":"el", "hindi":"hi", "italian":"it", "japanese":"ja",
    "korean":"ko", "norwegian":"no", "polish":"pl", "portugese":"pt",
    "romanian":"ro", "russian":"ru", "spanish":"es", "swedish":"sv" }

def setUserAgent(userAgent):
    urllib.FancyURLopener.version = userAgent
    pass

def translate(text, fromLang, toLang):
    setUserAgent("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008070400 SUSE/3.0.1-0.1 Firefox/3.0.1")
    try:
        postParameters = urllib.urlencode({"langpair":"%s|%s" %(langCode[fromLang.lower()],langCode[toLang.lower()]), "text":text,"ie":"UTF8", "oe":"UTF8"})
    except KeyError, error:
        print "Currently we do not support %s" %(error.args[0])
        return

    page = urllib.urlopen("http://translate.google.com/translate_t", postParameters)
    content = page.read()
    page.close()

    htmlSource = BeautifulSoup(content)
    translation = htmlSource.find('span', title=text )
    return translation.renderContents()


print translate("Good morning to you friend!", "English", "German")
print translate("Good morning to you friend!", "English", "Italian")
print translate("Good morning to you friend!", "English", "Spanish")

Guten Morgen, du Freund!
Buongiorno a te amico!
Buenos días a ti amigo!

How do I manage the letters that aren’t basic english letters? How would you recommend I solve this? I was thinking a dictionary to replace certain chains with another character, but I’m sure Python has something like this already. Batteries included and whatnot. 😛

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:01:28+00:00Added an answer on May 17, 2026 at 9:01 pm

    Don’t parse http://translate.google.com/translate_t since Google provides an AJAX service for this purpose. The translatedText in the json data returned by ajax.googleapis.com is already a unicode string.

    import urllib2
    import urllib
    import sys
    import json
    
    LANG={
        "arabic":"ar", "bulgarian":"bg", "chinese":"zh-CN",
        "croatian":"hr", "czech":"cs", "danish":"da", "dutch":"nl",
        "english":"en", "finnish":"fi", "french":"fr", "german":"de",
        "greek":"el", "hindi":"hi", "italian":"it", "japanese":"ja",
        "korean":"ko", "norwegian":"no", "polish":"pl", "portugese":"pt",
        "romanian":"ro", "russian":"ru", "spanish":"es", "swedish":"sv" }
    
    def translate(text,lang1,lang2):
        base_url='http://ajax.googleapis.com/ajax/services/language/translate?'    
        langpair='%s|%s'%(LANG.get(lang1.lower(),lang1),
                          LANG.get(lang2.lower(),lang2))
        params=urllib.urlencode( (('v',1.0),
                           ('q',text.encode('utf-8')),
                           ('langpair',langpair),) )
        url=base_url+params
        content=urllib2.urlopen(url).read()
        try: trans_dict=json.loads(content)
        except AttributeError:
            try: trans_dict=json.load(content)    
            except AttributeError: trans_dict=json.read(content)
        return trans_dict['responseData']['translatedText']
    
    print translate("Good morning to you friend!", "English", "German")
    print translate("Good morning to you friend!", "English", "Italian")
    print translate("Good morning to you friend!", "English", "Spanish")
    

    yields

    Guten Morgen, du Freund!
    Buongiorno a te amico!
    Buenos días a ti amigo!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write my very first python script. This was working but
My Applescript and Python script are in the present working directory. Now I need
I started working on a little Python script for FTP recently. To start off
I'm working on a Python script that needs to create about 50 distinct temporary
I'm working on a python script that starts several processes and database connections. Every
I'm working on a simple GUI Python script to do some simple tasks on
I'm working on a Python script that transforms this: foo bar Into this: [[Component
I'm working on a PHP script that runs a Python script on the server.
I'm developing a simple program which makes a Python script executable, and I'm working
I'm working on a Bash shell script that runs several Python scripts like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.