从 http://assets URL 读取和保存图像

Reading and saving images from http://assets URLs

我正在尝试从不同的 URL 下载一堆图像并将它们本地保存在我的 PC 上。使用下面的前两个链接,我收到错误:HTTPError: Forbidden 并且我不知道为什么。会不会是因为网站对此有保护?

这是我使用的脚本:

import urllib.request
imgURL = "one of the links below"

urllib.request.urlretrieve(imgURL, "C:/Users/temp/pic1.jpg")

以下网址。前两个报错,最后一个不报错

  1. https://assets.ellosgroup.com/i/ellos/b?$eg$&$em$&$ep$&$ed$&n=ell_1679093-02_Fs&mw=566&fmt=webp

  2. http://searchengineland.com/wp-content/seloads/2014/07/google-logo-black-1920.jpg

  3. https://media.istockphoto.com/photos/niagara-falls-picture-id508345252?k=6&m=508345252&s=170667a&w=0&h=bfGaBsL8VfBTLqJxFPwm8cXGaZLsgL34Ga4JSrhTtN0=

就像你说的那样,它必须防止来自网站的机器人程序,解决方案是指定一个已知的用户代理

import urllib.request

opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)
imgURL = "one of your links"
urllib.request.urlretrieve(imgURL, "C:/Users/temp/pic1.jpg")