title: Python3: HTTP Error 302 while using urllib
body: <p>I want to read the value of different stocks from websites. Therefore I wrote this tiny script, which reads the page source and then parses out the value:</p>
  
  <h2>stock_reader.py</h2>
  
  <pre><code>#!/usr/bin/env python3
  # -*- coding: utf-8 -*-
  
  from re import search
  from urllib import request
  
  
  def main():
      links = [
          [
              'CSG',
              'UBS',
          ],
          [
              'http://www.tradegate.de/orderbuch.php?isin=CH0012138530',
              'http://www.tradegate.de/orderbuch.php?isin=CH0244767585',
          ],
      ]
  
      for i in in range(len(links[0])):
          url = links[1][i]
          htmltext = request.urlopen(url).read().decode('utf-8')
          source = htmltext.splitlines()
          for line in source:
              if 'id="bid"' in line:
                  m = search('\d+.\d+', line)
                  print('{}'.format(m.string[m.start():m.end()]))
  
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>sometimes it works but sometimes this error gets raised:</p>
  
  <h2>error message</h2>
  
  <pre><code>Traceback (most recent call last):
    File "./aktien_reader.py", line 39, in &lt;module&gt;
      main()
    File "./aktien_reader.py", line 30, in main
      htmltext = request.urlopen(url).read().decode('utf-8')
    File "/usr/lib/python3.3/urllib/request.py", line 160, in urlopen
      return opener.open(url, data, timeout)
    File "/usr/lib/python3.3/urllib/request.py", line 479, in open
      response = meth(req, response)
    File "/usr/lib/python3.3/urllib/request.py", line 591, in http_response
      'http', request, response, code, msg, hdrs)
    File "/usr/lib/python3.3/urllib/request.py", line 511, in error
      result = self._call_chain(*args)
    File "/usr/lib/python3.3/urllib/request.py", line 451, in _call_chain
      result = func(*args)
    File "/usr/lib/python3.3/urllib/request.py", line 696, in http_error_302
      return self.parent.open(new, timeout=req.timeout)
    File "/usr/lib/python3.3/urllib/request.py", line 479, in open
      response = meth(req, response)
    File "/usr/lib/python3.3/urllib/request.py", line 591, in http_response
      'http', request, response, code, msg, hdrs)
    File "/usr/lib/python3.3/urllib/request.py", line 511, in error
      result = self._call_chain(*args)
    File "/usr/lib/python3.3/urllib/request.py", line 451, in _call_chain
      result = func(*args)
    File "/usr/lib/python3.3/urllib/request.py", line 696, in http_error_302
      return self.parent.open(new, timeout=req.timeout)
    File "/usr/lib/python3.3/urllib/request.py", line 479, in open
      response = meth(req, response)
    File "/usr/lib/python3.3/urllib/request.py", line 591, in http_response
      'http', request, response, code, msg, hdrs)
    File "/usr/lib/python3.3/urllib/request.py", line 511, in error
      result = self._call_chain(*args)
    File "/usr/lib/python3.3/urllib/request.py", line 451, in _call_chain
      result = func(*args)
    File "/usr/lib/python3.3/urllib/request.py", line 696, in http_error_302
      return self.parent.open(new, timeout=req.timeout)
    File "/usr/lib/python3.3/urllib/request.py", line 479, in open
      response = meth(req, response)
    File "/usr/lib/python3.3/urllib/request.py", line 591, in http_response
      'http', request, response, code, msg, hdrs)
    File "/usr/lib/python3.3/urllib/request.py", line 511, in error
      result = self._call_chain(*args)
    File "/usr/lib/python3.3/urllib/request.py", line 451, in _call_chain
      result = func(*args)
    File "/usr/lib/python3.3/urllib/request.py", line 696, in http_error_302
      return self.parent.open(new, timeout=req.timeout)
    File "/usr/lib/python3.3/urllib/request.py", line 479, in open
      response = meth(req, response)
    File "/usr/lib/python3.3/urllib/request.py", line 591, in http_response
      'http', request, response, code, msg, hdrs)
    File "/usr/lib/python3.3/urllib/request.py", line 511, in error
      result = self._call_chain(*args)
    File "/usr/lib/python3.3/urllib/request.py", line 451, in _call_chain
      result = func(*args)
    File "/usr/lib/python3.3/urllib/request.py", line 686, in http_error_302
      self.inf_msg + msg, headers, fp)
  urllib.error.HTTPError: HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop.
  The last 30x error message was:
  Found
  </code></pre>
  
  <p>My question is: why is it happening and how can I avoid it?</p>