So I have been trying out coding and am currently finding some language detection packages and found out about textblob, but I am having some sort of proble.
This is my code:
# - *- coding: utf- 8 - *-
from textblob import TextBlob
blob = TextBlob("Comment vas-tu?")
print(blob.detect_language())
print(blob.translate(to='es'))
print(blob.translate(to='en'))
print(blob.translate(to='zh'))
and this error shows:
Traceback (most recent call last):
File "C:\Users\*****\PycharmProjects\pythonProject\main.py", line 6, in <module>
print(blob.detect_language())
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\site-packages\textblob\blob.py", line 568, in detect_language
return self.translator.detect(self.raw)
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\site-packages\textblob\translate.py", line 72, in detect
response = self._request(url, host=host, type_=type_, data=data)
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\site-packages\textblob\translate.py", line 92, in _request
resp = request.urlopen(req)
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 214, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 523, in open
response = meth(req, response)
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 632, in http_response
response = self.parent.error(
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 561, in error
return self._call_chain(*args)
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain
result = func(*args)
File "C:\Users\*****\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 404: Not Found
Process finished with exit code 1
I am still a little bit of a beginner in programming... Can I ask what I can do to solve this?
You can make it working by doing some changes in your translate.py file as mentioned below:
Original:
url = "http://translate.google.com/translate_a/t?client=webapp&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&ie=UTF-8&oe=UTF-8&otf=2&ssel=0&tsel=0&kc=1"
Change above code to:
url = "http://translate.google.com/translate_a/t?client=te&format=html&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&ie=UTF-8&oe=UTF-8&otf=2&ssel=0&tsel=0&kc=1"
For further details visit this link: "HTTPError: HTTP Error 404: Not Found" while using translation function in TextBlob
Since you only need to do language detection, I see that langdetect library is a good fit:
https://pypi.org/project/langdetect/
Here is a demo on how to use it:
>>> from langdetect import detect
>>> detect('هيا بنا نلعب')
'ar'
>>>
Related
Hi i am newish to python, and working on modeling of the CV-19 outbreak in the UK.
Currently I am writting a program to automatically download the UK governments latest death statistics.
Here is my code so far:
import wget
url = "https://coronavirus.data.gov.uk/downloads/csv/coronavirus-cases_latest.csv"#
wget.download(url, 'C:/Users/Moshe/Downloads/Covid_19_uk_timeseries.csv')
Running this in pycharm I get:
C:\Users\Moshe\PycharmProjects\HelloWorld\venv\Scripts\python.exe C:/Users/Moshe/.PyCharmCE2019.1/config/scratches/Get_CV19_UK_stats.py
Traceback (most recent call last):
File "C:/Users/Moshe/.PyCharmCE2019.1/config/scratches/Get_CV19_UK_stats.py", line 6, in <module>
wget.download(url, 'C:/Users/Moshe/Downloads/Covid_19_uk_timeseries.csv')
File "C:\Users\Moshe\PycharmProjects\HelloWorld\venv\lib\site-packages\wget.py", line 526, in download
(tmpfile, headers) = ulib.urlretrieve(binurl, tmpfile, callback)
File "C:\Users\Moshe\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 247, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "C:\Users\Moshe\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Moshe\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\Moshe\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\Moshe\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Users\Moshe\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\Moshe\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 308: Permanent Redirect
Process finished with exit code 1
The file I am trying to get can be found on this page:
https://coronavirus.data.gov.uk/?_ga=2.157835066.1251021075.1589887735-1783596499.1585566366
Why won't this work? and what do i need to change?
Can you verify that your output path is valid and writeable?
try
import wget
import os
output_dir = 'C:/Users/Moshe/Downloads'
output_file = 'Covid_19_uk_timeseries.csv'
assert os.path.exists(output_dir)
url = "https://coronavirus.data.gov.uk/downloads/csv/coronavirus-cases_latest.csv"#
wget.download(url, out = os.path.join(output_dir,output_file))
I get an error when i try to download a video from youtube with pytube.
from pytube import YouTube
yt = YouTube('https://www.youtube.com/watch?v=9bZkp7q19f0')
stream = yt.streams.first()
stream
stream.download()
The error says "Forbidden", but fetching e.g. with curl from the same URL seems to work just fine. What could be wrong here?
Traceback (most recent call last):
File "C:/Users/Mr. jarvis/Desktop/youtube/youtube.py", line 7, in <module>
stream.download()
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\streams.py", line 217, in download
bytes_remaining = self.filesize
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\streams.py", line 164, in filesize
headers = request.get(self.url, headers=True)
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\request.py", line 21, in get
response = urlopen(url)
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\Mr. jarvis\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
Use pytube3 instead, it fixes this error (but is Python 3 only): https://github.com/hbmartin/pytube3
Experienced the same problem. I found unfinished downloaded file in the project folder. After deleting the unfinished downloaded file and running stream.download() again, it worked. So maybe worth checking.
pytube version 12.0.0
Mod security blocks me from accessing a URL.
I have tried using other methods from this website, but none seem to work as I use an import called PyCurrency-Calculator
import PyCurrency_Converter
import urllib.request
from bs4 import BeautifulSoup
PyCurrency_Converter.convert(1, 'USD', 'A$')
The error is:
Traceback (most recent call last):
File "E:/Downloads/list of currencies.py", line 7, in <module>
PyCurrency_Converter.convert(1, 'USD', 'A$')
File "E:\Python34\lib\site-packages\PyCurrency_Converter\PyCurrency.py",
line 48, in convert
return PyCurrency.convert(amount, _from, _to)
File "E:\Python34\lib\site-packages\PyCurrency_Converter\PyCurrency.py",
line 31, in convert
response = urllib2.urlopen(url)
File "E:\Python34\lib\urllib\request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "E:\Python34\lib\urllib\request.py", line 469, in open
response = meth(req, response)
File "E:\Python34\lib\urllib\request.py", line 579, in http_response
'http', request, response, code, msg, hdrs)
File "E:\Python34\lib\urllib\request.py", line 507, in error
return self._call_chain(*args)
File "E:\Python34\lib\urllib\request.py", line 441, in _call_chain
result = func(*args)
File "E:\Python34\lib\urllib\request.py", line 587, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
PyCurrency_Converter makes a call google finance under the hood (code). This service is no longer available and so the library needs to be modified to use a different service.
What you can do is find an alternative web based api and call that (I have no recommendation). As you can see the code for PyCurrency_Converter is quite short, so it will be simple to take and adapt it to another currency conversion api or to use requests or urllib to just do the requests yourself.
I try to learn python network programming from book "Foundation of Python Network Programming" Apres,
first we learn about 'virtualenv' and I install virtualenv , try to get 'googlemap package' and try using simple program call 'search1' :
#!/usr/bin/env python
# Foundations of Python Network Programming - Chapter 1 - search1.py
from googlemaps import GoogleMaps
address = '207 N. Defiance St, Archbold, OH'
print GoogleMaps().address_to_latlng(address)
after that I try running it,
the result from the book is
$ python search1.py
(41.5228242, -84.3063479)
the result I get is error liket this
Traceback (most recent call last):
File "search1.py", line 6, in
print GoogleMaps().address_to_latlng(address)
File "/home/administrator/gmapenv/local/lib/python2.7/site-packages/googlemaps.py", line 310, in address_to_latlng
return tuple(self.geocode(address)['Placemark'][0]['Point']['coordinates'][1::-1])
File "/home/administrator/gmapenv/local/lib/python2.7/site-packages/googlemaps.py", line 259, in geocode
url, response = fetch_json(self._GEOCODE_QUERY_URL, params=params)
File "/home/administrator/gmapenv/local/lib/python2.7/site-packages/googlemaps.py", line 50, in fetch_json
response = urllib2.urlopen(request)
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 407, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 520, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 445, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 528, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden
What make this error , because it I run down step by step like in the book
Hello fellow Programmers,
today I wanted to get some JSON Data from this website using Python 3.3: http://ladv.de/api/-apikey-redacted-/ausDetail?id=884&wettbewerbe=true&all=true
The official API tells me that calling this URL returns some JSON Data. But if I use the following code to get it (which I found on stackoverflow, too), it throws an error:
import urllib.request
import json
request = 'http://ladv.de/api/mmetzger/ausDetail?id=884&wettbewerbe=true&all=true'
response = urllib.request.urlopen(request)
obj = json.load(response)
str_response = response.readall().decode('utf-8')
obj = json.loads(str_response)
print(obj)
prints out
Traceback (most recent call last):
File "D:/ladvclient/testscrape.py", line 5, in <module>
response = urllib.request.urlopen(request)
File "C:\Python33\lib\urllib\request.py", line 156, in urlopen
return opener.open(url, data, timeout)
File "C:\Python33\lib\urllib\request.py", line 475, in open
response = meth(req, response)
File "C:\Python33\lib\urllib\request.py", line 587, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python33\lib\urllib\request.py", line 513, in error
return self._call_chain(*args)
File "C:\Python33\lib\urllib\request.py", line 447, in _call_chain
result = func(*args)
File "C:\Python33\lib\urllib\request.py", line 595, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
Where is the bug, and what is the correct code?
Thanks in advance,
forumfresser
The site you're trying to fetch is not available, as seen here:
http://ladv.de/api/-apikey-redacted-/ausDetail?id=884&wettbewerbe=true&all=true
You could also just read the error message by yourself:
urllib.error.HTTPError: HTTP Error 404: Not Found