how to keep script running despite urlib.error.HTTPerror, http.client.HTTPResponse o

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gintare
    New Member
    • Mar 2007
    • 103

    how to keep script running despite urlib.error.HTTPerror, http.client.HTTPResponse o

    I would like that script would keep running, i.e. requesting urls with different words, depite errors.
    Currently script stops running if error occurs.
    Code:
    #word comes from the list of words
    res6="http://glosbe.com/en/el/"+word
    try:
      resp = urllib.request.urlopen(res)
      if resp.getcode() == 200:
          html = resp.read()
          #proceed with result...
    except urlib.error.HTTPerror as e :
      print(str(e.getcode()))
      #proceed with error...
    Last edited by bvdet; Aug 16 '14, 02:06 PM. Reason: Fix indentation
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Encapsulate the request for urls in a while loop. Break the loop with a valid result to proceed.

    Comment

    • gintare
      New Member
      • Mar 2007
      • 103

      #3
      Thanks. The final code looks like:

      Code:
       
          #word comes from the list of words
          res6="http://glosbe.com/en/el/"+word
          try:
            while(urllib.request.urlopen(res)):
              resp = urllib.request.urlopen(res)
              if resp.getcode() == 200 : break 
            if resp.getcode() == 200:
            html = resp.read()
            #proceed with result...
          except urllib.error.HTTPError as e :
            #proceed with error...

      Comment

      Working...