Downloading Images from google response

I am trying to download images which I received through Google Form. I have copied the image url from google sheet to Excel Sheet.

My code is as below :

import urllib.request

from openpyxl import load_workbook
print(‘Cheking Download Status of the Images file…’)

wb=load_workbook(“Students Details.xlsx”)
sheet1=wb[“Sheet1”]
mkro=sheet1.max_row

for i in range(1,mkro):
download_status=sheet1.cell(row=i+1,column=3).value

url=(sheet1.cell(row=i+1,column=1).value)
savefileas='D:/Examination/CBSE/Registration/2021-22/Std.XI/Students Photos/Temp - Photographs/'+(sheet1.cell(row=i+1,column=2).value)+'.jpg'
    
if download_status==None:
    opener=urllib.request.build_opener()
    opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
    urllib.request.install_opener(opener)
    print('Connecting Server to download the image')
    urllib.request.urlretrieve(url,savefileas, )
    
    print('Photograph of '+str(i)+' - '+sheet1.cell(row=i+1,column=2).value+' downloaded successfully')
    sheet1.cell(row=i+1,column=3).value='downloaded successfully'
    wb.save("Students Details.xlsx")
    
    
elif download_status=='downloaded successfully':
    print(str(i)+'-> '+sheet1.cell(row=i+1,column=2).value+' Already downloaded')

After downloading 50 images it gives the follwoing error.:

Traceback (most recent call last):
File “D:\Examination\CBSE\Registration\2021-22\Std.XI\Students Photos\Download _Image_from _Google respose _link.py”, line 21, in
urllib.request.urlretrieve(url,savefileas, )
File “C:\Users\PANKAJ\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 239, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File “C:\Users\PANKAJ\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 214, in urlopen
return opener.open(url, data, timeout)
File “C:\Users\PANKAJ\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 523, in open
response = meth(req, response)
File “C:\Users\PANKAJ\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 632, in http_response
response = self.parent.error(
File “C:\Users\PANKAJ\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 561, in error
return self._call_chain(*args)
File “C:\Users\PANKAJ\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 494, in _call_chain
result = func(*args)
File “C:\Users\PANKAJ\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 641, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

How to solve this problem to continuous download till last url.

One of your URLs seem to hit the HTTP Error 403: Forbidden error, so you could wrap your code into a try/except block and skip this file.

PS: As this question is not specifically PyTorch-related you might get a faster answer in a Python board :wink: