【问题标题】:"403 Forbidden" when use python urlib package to download the image使用 python urllib 包下载图像时出现“403 Forbidden”
【发布时间】:2016-08-20 15:32:23
【问题描述】:

我是 urllib 包的新手。 我尝试下载网站“http://www.girl-atlas.com/album/576545de58e039318beb37f6”中的所有图片 问题是:当我复制图像的 url 并将 url 传递给浏览器时,我会收到错误 “403 Forbidden”。但是,当我在浏览器中右键单击图像并选择在新窗口中打开图像时,这一次,我将在新窗口中获取图像。 问题是:urllib如何模拟第二种方式?

【问题讨论】:

    标签: python web-crawler


    【解决方案1】:

    禁止在浏览器之外使用 URL。为了确保这一点,浏览器总是发送一个引用者,即加载图像的站点。如果浏览器是用 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()
    

    【讨论】:

      猜你喜欢
      • 2020-06-22
      • 1970-01-01
      • 2021-12-08
      • 2012-02-22
      • 2017-06-16
      • 2017-04-15
      • 2014-05-22
      相关资源
      最近更新 更多