Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #47499

Re: problem with if then

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: problem with if then
Date 2013-06-09 17:35 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-03039C.17352009062013@news.panix.com> (permalink)
References <cd1be83a-f560-4024-90b3-697a51bb1bb0@g7g2000vbv.googlegroups.com> <roy-DD0DA2.16235509062013@news.panix.com> <20165c85-4cc3-4b79-943b-82443e4a9bd0@w7g2000vbw.googlegroups.com>

Show all headers | View raw


In article 
<20165c85-4cc3-4b79-943b-82443e4a9bd0@w7g2000vbw.googlegroups.com>,
 Jean Dubois <jeandubois314@gmail.com> wrote:

> But, really,
> > once you've done all that (and it's worth doing as an exercise), rewrite
> > your code to use urllib2 or requests.  It'll be a lot easier.
> 
> Could you show me how to code the  example in metacode below wuth the
> use of urllib2?
> #!/usr/bin/env python
> import urllib2
> if check whether url exists succeed:
>     print 'url exists'
> else:
>     print 'url does not exist'

There are two basic ways Python function return status information.  
Either they return something which indicates failure (such as None), or 
they raise an exception.

In this case, what you want is urlopen(), which is documented at 
http://docs.python.org/2/library/urllib2.html.  The key piece of 
information is a couple of paragraphs down, where it says, "Raises 
URLError on errors".

It's not obvious from reading that whether URLError is a built-in or 
not, but that's easy enough to figure out:

>>> URLError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'URLError' is not defined
>>> import urllib2
>>> urllib2.URLError
<class 'urllib2.URLError'>

So, that means you want something like:

#!/usr/bin/env python

import urllib2

url = "http://whatever...."
try:
   urllib2.urlopen(url)
   print "url exists"
except urllib2.URLError:
   print "url does not exist"

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[newbie] problem with if then Jean Dubois <jeandubois314@gmail.com> - 2013-06-09 12:44 -0700
  Re: [newbie] problem with if then Fábio Santos <fabiosantosart@gmail.com> - 2013-06-09 21:00 +0100
    Re: problem with if then Jean Dubois <jeandubois314@gmail.com> - 2013-06-09 13:15 -0700
      Re: problem with if then Fábio Santos <fabiosantosart@gmail.com> - 2013-06-09 21:29 +0100
        Re: problem with if then Jean Dubois <jeandubois314@gmail.com> - 2013-06-09 13:51 -0700
  Re: [newbie] problem with if then Roy Smith <roy@panix.com> - 2013-06-09 16:23 -0400
    Re: problem with if then Jean Dubois <jeandubois314@gmail.com> - 2013-06-09 14:00 -0700
      Re: problem with if then Roy Smith <roy@panix.com> - 2013-06-09 17:35 -0400
        Re: problem with if then Jean Dubois <jeandubois314@gmail.com> - 2013-06-10 00:46 -0700
  Re: [newbie] problem with if then Albert Dengg <albert@fsfe.org> - 2013-06-09 22:37 +0200

csiph-web