Search code examples
pythontranslation

Is there a way to translate strings from one language to another using python for free without a daily limit?


I am making a web scraper where i need to translate words from english to dutch. I am currently using this module: https://pypi.org/project/translate/. After a while I run out of free daily translations. Is there a way to do this for free, possibly locally or using a different method?


Solution

  • Not sure if Google Translate limits the number of daily translations you can make - I know that through their official API, you need an API key, and it's probably limited - but what about the API your browser talks to when you translate something via Google search?

    def main():
    
        import requests
    
        url = "https://translate.google.com/translate_a/single"
    
        english_sentence = "I am hungry"
        from_language = "en"
        to_language = "de"
    
        params = {
            "client": "webapp",
            "sl": "auto",
            "tl": to_language,
            "hl": from_language,
            "dt": "t",
            "tk": "374347.226425",
            "q": english_sentence
        }
    
        response = requests.get(url, params=params)
        response.raise_for_status()
    
        data = response.json()
    
        german_sentence = data[0][0][0]
    
        print(german_sentence)
    
        return 0
    
    
    if __name__ == "__main__":
        import sys
        sys.exit(main())
    

    Output:

    Ich bin hungrig
    >>>