TextBlob
TextBlob copied to clipboard
Detecting language / get HTTP Error 400?
Hello - i try to detect a language from a word with this code:
from textblob import TextBlob
b = TextBlob("bonjour")
print(b.detect_language())
But unfortunately i get this error:
$ python exmplTextBlob.py
Traceback (most recent call last):
File "C:\Users\Polzi\Documents\DEV\Python-Diverses\Textblob\exmplTextBlob.py", line 4, in <module>
print(b.detect_language())
File "C:\Users\Polzi\Documents\DEV\.venv\test\lib\site-packages\textblob\blob.py", line 597, in detect_language
return self.translator.detect(self.raw)
File "C:\Users\Polzi\Documents\DEV\.venv\test\lib\site-packages\textblob\translate.py", line 76, in detect
response = self._request(url, host=host, type_=type_, data=data)
File "C:\Users\Polzi\Documents\DEV\.venv\test\lib\site-packages\textblob\translate.py", line 96, in _request
resp = request.urlopen(req)
File "C:\Users\Polzi\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 214, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Polzi\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 523, in open
response = meth(req, response)
File "C:\Users\Polzi\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 632, in http_response
response = self.parent.error(
File "C:\Users\Polzi\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 561, in error
return self._call_chain(*args)
File "C:\Users\Polzi\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain
result = func(*args)
File "C:\Users\Polzi\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 641, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
Why is that - am i doing anything wrong?
I'm getting this error too. Some SO posts I read point to it being a bad URL, but one commenter linked a PR that suggested the issue was fixed. Maybe it's resurfaced, or maybe this is something entirely new?
Hello,
@Rapid1898-code what kind of version of textblob are you using? Starting from the version 0.16.0 of textblob, as specified in the changelog (available here https://github.com/sloria/TextBlob/blob/dev/CHANGELOG.rst),
TextBlob.translate() and TextBlob.detect_language are deprecated. Use the official Google Translate API instead.
This might be the reason why you are getting this error.
Why this error comes
Its because that textblob uses a website Click here to go on that page.
That is why you get this error.
What is the solution
You can use selenium or requests modules for web scraping to use google translator
translation and language detection have been removed from textblob. it is recommended to use the official google translate API instead.