0

I am trying to use this google translate python library googletrans 3.0.0, which I installed from pypi.

I used this code to start with:

from googletrans import Translator

proxies = {'http': 'http://myproxy.com:8080', 'https': 'http://myproxy.com:8080'}
translator = Translator(proxies=proxies)
translator.translate("colour")

When I call the translator in the last line above, I got this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/alpha/miniconda3/lib/python3.9/site-packages/googletrans/client.py", line 182, in translate
    data = self._translate(text, dest, src, kwargs)
  File "/home/alpha/miniconda3/lib/python3.9/site-packages/googletrans/client.py", line 78, in _translate
    token = self.token_acquirer.do(text)
  File "/home/alpha/miniconda3/lib/python3.9/site-packages/googletrans/gtoken.py", line 194, in do
    self._update()
  File "/home/alpha/miniconda3/lib/python3.9/site-packages/googletrans/gtoken.py", line 54, in _update
    r = self.client.get(self.host)
  File "/home/alpha/miniconda3/lib/python3.9/site-packages/httpx/_client.py", line 755, in get
    return self.request(
  File "/home/alpha/miniconda3/lib/python3.9/site-packages/httpx/_client.py", line 600, in request
    return self.send(
  File "/home/alpha/miniconda3/lib/python3.9/site-packages/httpx/_client.py", line 620, in send
    response = self.send_handling_redirects(
  File "/home/alpha/miniconda3/lib/python3.9/site-packages/httpx/_client.py", line 647, in send_handling_redirects
    response = self.send_handling_auth(
  File "/home/alpha/miniconda3/lib/python3.9/site-packages/httpx/_client.py", line 684, in send_handling_auth
    response = self.send_single_request(request, timeout)
  File "/home/alpha/miniconda3/lib/python3.9/site-packages/httpx/_client.py", line 714, in send_single_request
    ) = transport.request(
AttributeError: 'str' object has no attribute 'request'

Is it the way I am inputting the proxies information to the Translator that makes it unhappy?

2 Answers 2

2

This seems to be very confusing according to the official docs, but this github issue has a solution.

For some reason the docs specify both strings and HTTPTransports but this has been clarified in the issue above.

Basically:

from httpcore import SyncHTTPProxy
from googletrans import Translator

http_proxy = SyncHTTPProxy((b'http', b'myproxy.com', 8080, b''))
proxies = {'http': http_proxy, 'https': http_proxy }

translator = Translator(proxies=proxies)
translator.translate("colour")
Sign up to request clarification or add additional context in comments.

3 Comments

I replaced 'myproxy.com' with my proxy IP address, like '123.456.789.101', and the 8080 with my proxy port number, but I got now File "/home/alpha/miniconda3/lib/python3.9/site-packages/googletrans/gtoken.py", line 62, in _update code = self.RE_TKK.search(r.text).group(1).replace('var ', '') AttributeError: 'NoneType' object has no attribute 'group'; which I think is a different problem now. Thanks for your help!
I've encountered that too, try pip install googletrans==4.0.0-rc1
I have tried to install this version and it worked, thanks again!
0

You can also set some environment variables.
For Windows:
cmd:

    set http_proxy=...
    set https_proxy=...

powershell:

    $env:http_proxy = ...; $env:https_proxy = ...

For Linux:

    export http_proxy=... https_proxy=...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.