Downloading Images from google response

Pankaj_Paul: <p>I am trying to download images which I received through Google Form. I have copied the image url from google sheet to Excel Sheet.</p>
<p>My code is as below :</p>
<p>import  urllib.request</p>
<p>from openpyxl import load_workbook<br>
print(‘Cheking Download Status of the Images file…’)</p>
<p>wb=load_workbook(“Students Details.xlsx”)<br>
sheet1=wb[“Sheet1”]<br>
mkro=sheet1.max_row</p>
<p>for i in range(1,mkro):<br>
download_status=sheet1.cell(row=i+1,column=3).value</p>
<pre><code>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)+'-&gt; '+sheet1.cell(row=i+1,column=2).value+' Already downloaded')
</code></pre>
<p>After downloading 50 images it gives the follwoing error.:</p>
<p>Traceback (most recent call last):<br>
File “D:\Examination\CBSE\Registration\2021-22\Std.XI\Students Photos\Download _Image_from _Google respose _link.py”, line 21, in <br>
urllib.request.urlretrieve(url,savefileas, )<br>
File “C:\Users\PANKAJ\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 239, in urlretrieve<br>
with contextlib.closing(urlopen(url, data)) as fp:<br>
File “C:\Users\PANKAJ\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 214, in urlopen<br>
return opener.open(url, data, timeout)<br>
File “C:\Users\PANKAJ\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 523, in open<br>
response = meth(req, response)<br>
File “C:\Users\PANKAJ\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 632, in http_response<br>
response = self.parent.error(<br>
File “C:\Users\PANKAJ\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 561, in error<br>
return self._call_chain(*args)<br>
File “C:\Users\PANKAJ\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 494, in _call_chain<br>
result = func(*args)<br>
File “C:\Users\PANKAJ\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 641, in http_error_default<br>
raise HTTPError(req.full_url, code, msg, hdrs, fp)<br>
urllib.error.HTTPError: HTTP Error 403: Forbidden</p>
<p>How to solve this problem to continuous download till last url.</p>

ptrblck: <p>One of your URLs seem to hit the <code>HTTP Error 403: Forbidden</code> error, so you could wrap your code into a <code>try/except</code> block and skip this file.</p>
<p>PS: As this question is not specifically PyTorch-related you might get a faster answer in a Python board <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=10" title=":wink:" class="emoji" alt=":wink:"></p>