title: Download bulk images in python
body: <p>After watching a video about how to download images using python, I typed the code in the video and here's the code</p>
  <pre><code>import pandas as pd
  import urllib.request
  
  def url_to_jpg(i, url, file_path):
      filename = 'image-{}.jpg'.format(i)
      fullpath = '{}{}'.format(file_path, filename)
      print(fullpath)
      urllib.request.urlretrieve(url, fullpath)
      print('{} saved.'.format(filename))
      return None
  
  FILENAME = 'Images URLs.csv'
  FILE_PATH = 'Images/'
  urls = pd.read_csv(FILENAME)
  
  for i, url in enumerate(urls.values):
      url_to_jpg(i, url, FILE_PATH)
  </code></pre>
  <p>When testing the code, I encountered error at this line
  <code>urllib.request.urlretrieve(url, fullpath)</code> which is like that</p>
  <pre><code>Images/image-0.jpg
  ---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-36-d92ed57d1d8e&gt; in &lt;module&gt;
       15 
       16 for i, url in enumerate(urls.values):
  ---&gt; 17     url_to_jpg(i, url, FILE_PATH)
  
  &lt;ipython-input-36-d92ed57d1d8e&gt; in url_to_jpg(i, url, file_path)
        6     fullpath = '{}{}'.format(file_path, filename)
        7     print(fullpath)
  ----&gt; 8     urllib.request.urlretrieve(url, fullpath)
        9     print('{} saved.'.format(filename))
       10     return None
  
  C:\ProgramData\Anaconda3\lib\urllib\request.py in urlretrieve(url, filename, reporthook, data)
      243     data file as well as the resulting HTTPMessage object.
      244     &quot;&quot;&quot;
  --&gt; 245     url_type, path = _splittype(url)
      246 
      247     with contextlib.closing(urlopen(url, data)) as fp:
  
  C:\ProgramData\Anaconda3\lib\urllib\parse.py in _splittype(url)
     1006         _typeprog = re.compile('([^/:]+):(.*)', re.DOTALL)
     1007 
  -&gt; 1008     match = _typeprog.match(url)
     1009     if match:
     1010         scheme, data = match.groups()
  
  TypeError: cannot use a string pattern on a bytes-like object
  </code></pre>
  <p>Any ideas about that error?</p>
  <p>** I have found the solution to a point which is modifying this line
  <code>url_to_jpg(i, url[0], FILE_PATH)</code></p>
  <p>But it seems that some of the links are not allowed as I got another error
  <code>HTTPError: HTTP Error 403: Forbidden</code>
  How can I overcome this?</p>
  <p>** I tried to add headers (agent) as suggested but don't know how to finish it properly. How to use <code>urlretrieve</code> in that case?</p>
  <pre><code>import urllib.request
  
  hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
         'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
         'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
         'Accept-Encoding': 'none',
         'Accept-Language': 'en-US,en;q=0.8',
         'Connection': 'keep-alive'}
  
  response = urllib.request.Request(&quot;http://www.gunnerkrigg.com//comics/00000001.jpg&quot;, headers=hdr)
  print(urllib.request.urlopen(response))
  urllib.request.urlretrieve(urllib.request.urlopen(response).read(),'oo.jpg')
  #urllib.request.urlretrieve(&quot;http://www.gunnerkrigg.com//comics/00000001.jpg&quot;, &quot;00000001.jpg&quot;)
  </code></pre>