簡體   English   中英

Python3 Urllib映像下載返回HTTP錯誤403:禁止

[英]Python3 Urllib image download return HTTP Error 403: Forbidden

我正在處理一個python項目,需要從URL下載圖像,我在Google上搜索了很多,並嘗試了很多解決方案,但無法為我工作。

已更新:現在我將代碼更新為:

from PIL import Image
from flask import Flask
import requests
app = Flask(__name__)


@app.route('/<path:image_url>')
def grab_image(image_url):
    url = str(image_url)
    r = requests.get(url, allow_redirects=True)
    print('Url is as: {}'.format(url))
    filename = url.split('/')[-1]
    open(filename, 'wb').write(r.content)
    img = Image.open(filename)
    img.show()
    return img


if __name__ == '__main__':
    app.run()

現在,它下載圖像並顯示它,但沒有將圖像保存在我的目錄中,這可能是什么問題?

下面是以前的/舊的代碼。

這是我嘗試過的:

from flask import Flask
import urllib.request
app = Flask(__name__)


def download_img(image_url, file_path, file_name):
    full_path = file_path + file_name + '.jpg'
    urllib.request.urlretrieve(image_url, full_path)
    pass


@app.route('/<path:image_url>')
def hello_world(image_url):
    file_name = 'async'
    download_img(image_url, 'img/', file_name)
    return 'Hello World!'


if __name__ == '__main__':
    app.run()

這是我的要求:

http://127.0.0.1:5000/https://www.thelaurelmagazine.com/sites/default/files/styles/hero_image/public/mary_abryani_highlands_nc_yoga.jpg

但它返回此錯誤:

urllib.error.HTTPError: HTTP Error 403: Forbidden
127.0.0.1 - - [02/Sep/2018 13:13:57] "GET /https://www.thelaurelmagazine.com/sites/default/files/styles/hero_image/public/mary_abryani_highlands_nc_yoga.jpg HTTP/1.1" 500 -

我也嘗試使用http而不是https但是它返回相同的錯誤。

請幫幫我!

提前致謝!

必須在要發送的請求的標頭中指定用戶代理。

from flask import Flask
import urllib.request
from PIL import Image
app = Flask(__name__)

user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'


def download_img(image_url, file_path, file_name):
    full_path = file_path + file_name + '.jpg'
    headers={'User-Agent':user_agent,} 
    request=urllib.request.Request(image_url,None,headers)
    response = urllib.request.urlopen(request)
    #install PIL package to convert the response into a PIL Image object to further save it
    image=Image.open(response)
    image.save(full_path)
    pass


@app.route('/<path:image_url>')
def hello_world(image_url):
    file_name = 'async'
    download_img(image_url, 'img/', file_name)
    return 'Hello World!'


if __name__ == '__main__':
    app.run()

在上一個答案中,我誤解了您的問題,對不起。

img.show()

只會嘗試在屏幕上顯示圖像,而不會將其保存到工作目錄中。

您不會關閉已創建的文件,也不會將文件對象的鏈接分配給任何變量,因此創建后只是由垃圾收集器收集它

url = str(image_url)
r = requests.get(url, allow_redirects=True)
print('Url is as: {}'.format(url))
filename = url.split('/')[-1]
with open(filename, 'wb') as file:
    file.write(r.content)
return send_file(filename)

要么

url = str(image_url)
r = requests.get(url, allow_redirects=True)
print('Url is as: {}'.format(url))
filename = url.split('/')[-1]
file = open(filename, 'wb')
file.write(r.content)
file.close()
return send_file(filename)

現在應該解決您的問題(第一個變體的書面使用情況管理器with ,第二個是傳統的做法)
flask.send_file()文檔

我也建議不要在服務器端使用img.show() (如果您已將項目部署到遠程服務器,則不會有任何影響)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM