使用python urlib包下载图片时出现“403 Forbidden”
"403 Forbidden" when use python urlib package to download the image
我是 urllib 包的新手。
我尝试下载网站“http://www.girl-atlas.com/album/576545de58e039318beb37f6”中的所有图片
问题是:当我复制图像的 url,并将 url 传递给浏览器时,我会收到错误消息 “403 Forbidden”。但是,当我在浏览器中右键单击图片,并选择以新window打开图片时,这一次,我将在新window中打开图片。
问题是:urlib 如何模拟第二种方式?
禁止在浏览器外使用网址。为确保这一点,浏览器始终会发送一个引荐来源网址,即从中加载图像的站点。如果浏览器用 Python 编写,它看起来像这样:
import urllib.request
opener = urllib.request.URLopener()
opener.addheader('Referer', 'http://www.girl-atlas.com/album/576545de58e039318beb37f6')
image = opener.open('http://girlatlas.b0.upaiyun.com/41/20121222/234720feaa1fc912ba4e.jpg!lrg')
data = image.read()
image.close()
我是 urllib 包的新手。 我尝试下载网站“http://www.girl-atlas.com/album/576545de58e039318beb37f6”中的所有图片 问题是:当我复制图像的 url,并将 url 传递给浏览器时,我会收到错误消息 “403 Forbidden”。但是,当我在浏览器中右键单击图片,并选择以新window打开图片时,这一次,我将在新window中打开图片。 问题是:urlib 如何模拟第二种方式?
禁止在浏览器外使用网址。为确保这一点,浏览器始终会发送一个引荐来源网址,即从中加载图像的站点。如果浏览器用 Python 编写,它看起来像这样:
import urllib.request
opener = urllib.request.URLopener()
opener.addheader('Referer', 'http://www.girl-atlas.com/album/576545de58e039318beb37f6')
image = opener.open('http://girlatlas.b0.upaiyun.com/41/20121222/234720feaa1fc912ba4e.jpg!lrg')
data = image.read()
image.close()