2

My current program looks like this

import os
import urllib.request


baseUrl = "https://website.com/wp-content/upload/xxx/yyy/zzz-%s.jpg"

for i in range(1,48):
    url = baseUrl % i
    urllib.request.urlretrieve(baseUrl, os.path.basename(url))

I haven't coded python in a long time, but I wrote this using urllib2 back when I used to use Python2.7.

It is supposed to replace the %s in the URL and loop through 1-48, and download all the images to the directory that the script is in. But i get alot of errors.

edit : Here is the error that is thrown.

Traceback (most recent call last):
  File "download.py", line 9, in <module>
    urllib.request.urlretrieve(url, os.path.basename(url))
  File "C:\Program Files\Python37\lib\urllib\request.py", line 247, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "C:\Program Files\Python37\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Program Files\Python37\lib\urllib\request.py", line 531, in open
    response = meth(req, response)
  File "C:\Program Files\Python37\lib\urllib\request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Program Files\Python37\lib\urllib\request.py", line 569, in error
    return self._call_chain(*args)
  File "C:\Program Files\Python37\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Program Files\Python37\lib\urllib\request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

Reznik
  • 2,663
  • 1
  • 11
  • 31
  • notice you are passing `baseUrl` into the network call. `baseUrl` is not the `url`, it is your format string and still has the `zzz-%s.jpg` at the end. – RufusVS Jun 16 '20 at 04:07

3 Answers3

1

urllib.request is only available on Python 3 so you have to run the code in Python 3.

namgold
  • 1,009
  • 1
  • 11
  • 32
0

Simple fix, if you pass the correct string:

 urllib.request.urlretrieve(url, os.path.basename(url))

The documentation says urlretrieve is a Legacy carryover, so you might want to find a different way to do this.

I found this alternate approach modified from another SO answer:

import os
import requests
baseUrl = "https://website.com/wp-content/upload/xxx/yyy/zzz-%s.jpg"
for i in range(1,48):
    url = baseUrl % i
    r = requests.get(url)
    open(os.path.basename(url), 'wb').write(r.content)    
RufusVS
  • 4,008
  • 3
  • 29
  • 40
0

Try using the requests module:

import requests
baseUrl = "https://website.com/wp-content/upload/xxx/yyy/zzz-%s.jpg"

for i in range(1,48):
    url = baseUrl % i
    response = requests.get(url)
    my_raw_data = response.content
    with open(os.path.basename(url), 'wb') as my_data:
        my_data.write(my_raw_data)
    my_data.close()

Just to add, you must use url in the request, not the baseUrl as shown in your code :

import os
import urllib.request


baseUrl = "https://website.com/wp-content/upload/xxx/yyy/zzz-%s.jpg"

for i in range(1,48):
    url = baseUrl % i
    #urllib.request.urlretrieve(baseUrl, os.path.basename(url))
    #Use This line :
    urllib.request.urlretrieve(url, os.path.basename(url))

Run this in Python 3

Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40