[Tutor] Beautiful soup

Oliver Maunder lists at olivermaunder.co.uk
Tue Oct 4 22:49:23 CEST 2005


On 10/4/05, Andrew P <grouch at gmail.com> wrote:
>
> Oops, Paul is probably right. I thought urllib2 opened local files in the
> absence of an identifier like "http://". Bad assumption on my part. I
> remembered that behavior from somewhere else, maybe urllib.


The following function could be useful here - I got it from Dive Into Python
- http://diveintopython.org/scripts_and_streams/index.html#kgp.openanything

It tries to open a file with urllib, and if that fails it uses open():

def openAnything(source):
    # try to open with urllib (if source is http, ftp, or file URL)

    import urllib
    try:
        return urllib.urlopen
(source)
    except (IOError, OSError):
        pass


# try to open with native open function (if source is pathname)
    try:
        return open(source)

    except (IOError, OSError):
        pass


# treat source as string
    import StringIO
    return StringIO.StringIO(str(source))


Olly
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051004/9097fdbe/attachment.html


More information about the Tutor mailing list