8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

HTTP Error 403: Forbiddenの対処法

Last updated at Posted at 2021-03-03

#「ゼロから作るDeep Learning」を勉強中のことでした

3章『ニューラルネットワーク』中に3.6「手書き数字認識」の項.
mnistをダウンロードし表示させるmnist_show.pyを実行したところ,以下のようなエラーが...

urllib.error.HTTPError: HTTP Error 403: Forbidden

どうやら,mnist.py 中の関数 _downloadでエラーが発生してる.

mnist.py
def _download(file_name):
    file_path = dataset_dir + "/" + file_name

    if os.path.exists(file_path):
        return

    print("Downloading " + file_name + " ... ")
    urllib.request.urlretrieve(url_base + file_name, file_path)
    print("Done")

調べてみると,対象urlへのリクエスト権限がないとのこと.
urlのヘッダーを偽装することで解決するらしい.

#ソースコード修正
headersを適当に指定します.
それを使ってurllib.request.Requestオブジェクトを取得.
urlopenでファイルをダウンロードします.
*urlretreiveは引数に文字列strをとるので今回の方法では使用できませんでした

mnist.py
def _download(file_name):
    file_path = dataset_dir + "/" + file_name

    if os.path.exists(file_path):
        return

    print("Downloading " + file_name + " ... ")
    headers = {
        "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0"
        }
    request = urllib.request.Request(url_base+file_name, headers=headers)
    response = urllib.request.urlopen(request).read()
    with open(file_path, mode='wb') as f:
        f.write(response)
    print("Done")

#実行してみる

Downloading train-images-idx3-ubyte.gz ... 
Done
Downloading train-labels-idx1-ubyte.gz ... 
Done
Downloading t10k-images-idx3-ubyte.gz ... 
Done
Downloading t10k-labels-idx1-ubyte.gz ... 
Done
Converting train-images-idx3-ubyte.gz to NumPy Array ...
Done!
5
(784,)
(28, 28)

成功!

追記 2021-3-11

同じところで困ってる方が多かったのでリポジトリのissuesに投稿しました.

8
2
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?