使用python urllib从url下载图像但收到HTTP错误403:禁止 [英] download image from url using python urllib but receiving HTTP Error 403: Forbidden

查看:48
本文介绍了使用python urllib从url下载图像但收到HTTP错误403:禁止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 python 模块urllib.request"从 url 下载图像文件,该模块适用于某些网站(例如 mangastream.com),但不适用于另一个(mangadoom.co)接收错误HTTP 错误 403": 禁止".后一种情况可能有什么问题以及如何解决?

I want to download image file from a url using python module "urllib.request", which works for some website (e.g. mangastream.com), but does not work for another (mangadoom.co) receiving error "HTTP Error 403: Forbidden". What could be the problem for the latter case and how to fix it?

我在 OSX 上使用 python3.4.

I am using python3.4 on OSX.

import urllib.request

# does not work
img_url = 'http://mangadoom.co/wp-content/manga/5170/886/005.png'
img_filename = 'my_img.png'
urllib.request.urlretrieve(img_url, img_filename)

在错误消息的末尾它说:

At the end of error message it said:

... 
HTTPError: HTTP Error 403: Forbidden

但是,它适用于另一个网站

However, it works for another website

# work
img_url = 'http://img.mangastream.com/cdn/manga/51/3140/006.png'
img_filename = 'my_img.png'
urllib.request.urlretrieve(img_url, img_filename)

我已经尝试了以下帖子中的解决方案,但它们都不适用于 mangadoom.co.

I have tried the solutions from the post below, but none of them works on mangadoom.co.

通过 urllib 和 python 下载图片

如何在 python 中复制远程图像?

这里的解决方案也不适合,因为我的情况是下载图像.urllib2.HTTPError:HTTP 错误 403:禁止

The solution here also does not fit because my case is to download image. urllib2.HTTPError: HTTP Error 403: Forbidden

也欢迎使用非 Python 解决方案.您的建议将不胜感激.

Non-python solution is also welcome. Your suggestion will be very appreciated.

推荐答案

本网站阻止了 urllib 使用的用户代理,因此您需要在您的请求中更改它.不幸的是,我不认为 urlretrieve 直接支持这一点.

This website is blocking the user-agent used by urllib, so you need to change it in your request. Unfortunately I don't think urlretrieve supports this directly.

我建议使用漂亮的 requests 库,代码变为(来自 此处) :

I advise for the use of the beautiful requests library, the code becomes (from here) :

import requests
import shutil

r = requests.get('http://mangadoom.co/wp-content/manga/5170/886/005.png', stream=True)
if r.status_code == 200:
    with open("img.png", 'wb') as f:
        r.raw.decode_content = True
        shutil.copyfileobj(r.raw, f)

请注意,该网站似乎不禁止 requests 用户代理.但是如果需要修改它很容易:

Note that it seems this website does not forbide requests user-agent. But if need to be modified it is easy :

r = requests.get('http://mangadoom.co/wp-content/manga/5170/886/005.png',
                 stream=True, headers={'User-agent': 'Mozilla/5.0'})

同样相关:更改 urllib 中的用户代理

这篇关于使用python urllib从url下载图像但收到HTTP错误403:禁止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆