简体   繁体   English

无法使用 python 下载图像

[英]can't download image with python

try to download images with python but only this picture can't download it尝试使用 python 下载图像,但只有这张图片无法下载
i don't know the reason cause when i run it, it just stop just nothing happen no image, no error code...我不知道原因,当我运行它时,它只是停止,什么也没发生,没有图像,没有错误代码......

here's the code plz tell me the reason and solution plz..这是代码请告诉我原因和解决方案请告诉我..

import urllib.request

num=404

def down(URL):

    fullname=str(num)+"jpg"
    urllib.request.urlretrieve(URL,fullname)
    im="https://www.thesun.co.uk/wp-content/uploads/2020/09/67d4aff1-ddd0-4036-a111-3c87ddc0387e.jpg"

down(im)

What @MoetazBrayek says in their comment (but not answer) is correct: the website you're querying is blocking the request. @MoetazBrayek 在他们的评论(但没有回答)中所说的是正确的:您正在查询的网站正在阻止请求。

It's common for sites to block requests based on user-agent or referer: if you try to curl https://www.thesun.co.uk/wp-content/uploads/2020/09/67d4aff1-ddd0-4036-a111-3c87ddc0387e.jpg you will get an HTTP error (403 Access Denied):网站通常会根据用户代理或引用者来阻止请求:如果您尝试curl https://www.thesun.co.uk/wp-content/uploads/2020/09/67d4aff1-ddd0-4036-a111-3c87ddc0387e.jpg你会得到一个 HTTP 错误(403 Access Denied):

❯ curl -I https://www.thesun.co.uk/wp-content/uploads/2020/09/67d4aff1-ddd0-4036-a111-3c87ddc0387e.jpg
HTTP/2 403 

Apparently The Sun wants a browser's user-agent, and specifically the string "mozilla" is enough to get through:显然 The Sun 想要一个浏览器的用户代理,特别是字符串“mozilla”足以通过:

❯ curl -I -A mozilla https://www.thesun.co.uk/wp-content/uploads/2020/09/67d4aff1-ddd0-4036-a111-3c87ddc0387e.jpg
HTTP/2 200 

You will have to either switch to the requests package or replace your url string with a proper urllib.request.Request object so you can customise more pieces of the request.您将不得不切换到requests package 或用适当的urllib.request.Request object 替换您的 url 字符串,以便您可以自定义更多请求。 And apparently urlretrieve does not support Request objects so you will also have to use urlopen :显然urlretrieve不支持 Request 对象,因此您还必须使用urlopen

req = urllib.request.Request(URL, headers={'User-Agent': 'mozilla'})
res = urllib.request.urlopen(req)
assert res.status == 200
with open(filename, 'wb') as out:
    shutil.copyfileobj(res, out)

this code will work for you try to change the url that you use and see result:此代码适用于您尝试更改您使用的 url 并查看结果:

import requests

pic_url = "https://www.thesun.co.uk/wp-content/uploads/2020/09/67d4aff1-ddd0-4036-a111-3c87ddc0387e.jpg"
cookies = dict(BCPermissionLevel='PERSONAL')


with open('aa.jpg', 'wb') as handle:
        response = requests.get(pic_url, headers={"User-Agent": "Mozilla/5.0"}, cookies=cookies,stream=True)
        if not response.ok:
            print (response)

        for block in response.iter_content(1024):
            if not block:
                break

            handle.write(block)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM