1
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.

"urllib.error.HTTPError: HTTP Error 403: Forbidden"の対処法

Last updated at Posted at 2021-07-06

<事象説明>
"urllib.error.HTTPError: HTTP Error 403: Forbidden"エラーは
閲覧権限がないを意味する。
サイト側がブラウザ以外のアクセスを拒否する設定になっている場合に起きる。

<解決方法>
ヘッダーをつけて、ブラウザに偽装する

<コード例1>.py
url = 'https://www.a.com/abc.csv'
save_path = '\savename.csv'
opener=urllib.request.build_opener()
opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
urllib.request.install_opener(opener)
urllib.request.urlretrieve(url, save_path)
<コード例2>.py
opener = urllib.request.build_opener()
opener.addheaders = [('User-Agent',
                      'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]

urllib.request.install_opener(opener)

for img in img_nodes:
    if img.get('file') is not None:
        img_url = img.get('file')
        ID = img.get('id')
        line = str(count) + "\t" + img_url  + "\n"
        img_meta.write(line)
        try:
           #urllib.request.urlretrieve(img_url, "./output/image/" + str(count) + ".jpg") # 画像を一つづつダウンロード
           #urllib.request.urlretrieve(img_url, "./output/image/" + ID + ".jpg")
           urllib.request.urlretrieve(img_url, "./output/image/" + ID + ".jpg")
        except urllib.request.URLError:
            print("Error opening URL")
        count += 1

参考:https://blog.csdn.net/liumoude6/article/details/88074565

1
2
0

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
1
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?