3

I am trying to scrape a German website and I am in need to convert the scraped data from German to English. Now, What I did is I have hitted third party website to do this process for me. Hereby I have attached the code i tried as below:

from bs4 import BeautifulSoup, SoupStrainer
import urllib2
import urllib
import re
import sys
import string
import json
import socket


def translate(text_to_translate):
    base_url = 'http://translate.reference.com/german/english/'
    join_url = base_url + text_to_translate
    request = urllib2.Request(join_url)
    response = urllib2.urlopen(request)
    soup = BeautifulSoup(response)
    result = soup.find('textarea', {'placeholder': 'Translation'})
    converted_text = result.string
    return converted_text


text = "damen uhren"
text1 = re.sub('\s+', '-', text)
title_new = translate(text1)
print "Original String = ", text
print "Converted String = ", title_new

Is there a way to convert the german strings to english without hitting third-party websites? Is yes, please guide me through this.

Pang
  • 8,605
  • 144
  • 77
  • 113
  • consider using the deep_translator library, where many translators are integrated: https://pypi.org/project/deep-translator/ – basilisk Jul 26 '20 at 20:47

2 Answers2

5

You could use goslate (google translate python api). First, in the terminal, $pip install goslate

  import goslate
  gs = goslate.Goslate()
  new_word = gs.translate('my german sentence', 'de')
Peter Graham
  • 1,950
  • 1
  • 20
  • 26
4

goslate have acknowledged the ticket system introduced by google translate.

"Google has updated its translation service recently with a ticket mechanism to prevent simple crawler program like goslate from accessing. Though a more sophisticated crawler may still work technically, however it would have crossed the fine line between using the service and breaking the service. goslate will not be updated to break google’s ticket mechanism. Free lunch is over. Thanks for using."

http://pythonhosted.org/goslate/

You may have some luck with py-translate. I don't know how accurate the translations provided are. Likely it uses a dictionary.

https://pypi.python.org/pypi/py-translate

CodingMatters
  • 840
  • 10
  • 21
  • 1
    py-translate is showing deprecated on its git page. "This library has been deprecated due to changes in Google Terms of Agreement. Please use the Google Cloud Translation API" IS there any other option... – Manvi Feb 17 '17 at 19:17