Exception: URL fetch failure on https://www.manythings.org/anki/ita-eng.zip: 406 -- Not Acceptable

Viewed 38

I am trying to run a seq2seq model in google colab but when I try to load the dataset with the following command:

def download_NMT():
  path_to_zip = tf.keras.utils.get_file(
      fname='ita-eng.zip', origin='https://www.manythings.org/anki/ita-eng.zip', extract=True)
  
  path_to_file = os.path.dirname(path_to_zip) + '/ita-eng/ita.txt'
  return path_to_file

it doesn't work and gives me the following error:

HTTPError: HTTP Error 406: Not Acceptable

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/keras/utils/data_utils.py in get_file(fname, origin, untar, md5_hash, file_hash, cache_subdir, hash_algorithm, extract, archive_format, cache_dir)
    277         urlretrieve(origin, fpath, dl_progress)
    278       except urllib.error.HTTPError as e:
--> 279         raise Exception(error_msg.format(origin, e.code, e.msg))
    280       except urllib.error.URLError as e:
    281         raise Exception(error_msg.format(origin, e.errno, e.reason))

Exception: URL fetch failure on https://www.manythings.org/anki/ita-eng.zip: 406 -- Not Acceptable
1 Answers

Sometimes this happens when, on server side is checked whether you're a bot, or not. The server tries to get from which browser you’re making the request, and since you are not on any browser (even though you’re on google Colab, you’re making the request from python), it gives you the 406 error.

Usually, in order to trick the server, along with the URL is sent a header that simulates the browser.

From the tf.keras.utils.get_file documentation, however, I see no way of adding a header that enables you to do this. Also, as far as I know there is no way of adding an header directly to an URL.

Maybe you should resort to downloading the file differently. For example:

import requests
url = 'https://www.manythings.org/anki/ita-eng.zip'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}

r = requests.get(url, headers=headers)
# save the file:
open(‘my_file.zip’, 'wb').write(r.content)
Related