Python

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Download file from the web and store it locally.

    3 answers - 63 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit


    Tutor maillist - Tutor (AT) python (DOT) org
  • No.1 | | 2066 bytes | |

    Mon, 22 May 2006, MATATA EMMANUEL wrote:

    import urllib
    import sys
    # Get a file-like object from the web and store it locally.
    url = ""
    f = urllib.urlopen(url)
    # connect to the remote
    try:
    remote = urllib.urlopen(url)
    except:
    print "Connot open URL"
    sys.exit()

    Don't do this kind of exception handling: it hides information about the
    exception.

    If something bad happens, we want to see something other than "Cannot open
    URL". So instead, let the exception fall through:

    url = ""
    f = urllib.urlopen(url)
    remote = urllib.urlopen(url)

    at the very least, modify the exception handler to print out more
    informative error messages other than "An error happened". We can use the
    'traceback' module in the Standard Library to get us more informative
    error messages.

    While you're writing simple programs, I'd recommend avoiding exception
    handlers, just because if something bad happens, we need to preserve all
    the debugging information we can get.

    # Read from the object, storing the page's contents in 's' want to
    download parcels.zip.
    s = f.read(Parcels.zip)

    What is parcels.zip? I don't understand what this is trying to do yet.

    At this point, 'f' is a file whose contents contains the stuff in

    I get the feeling you're thinking like a web browser, in the sense that
    you need to first visit the page before downloading the file.

    If so, that's not it. *grin*

    If the file you're trying to download has a URL like:

    then there is no reason to have Python visit the downloads.htm page first:
    you can go ahead and grab Parcels.zip first, unless the content provider
    has done something weird to their web server.

    other comment:

    m = urllib.urlretrieve

    This won't fire off. In Python, functions that take no arguments still
    need parens to be called.

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.2 | | 1508 bytes | |

    I need help on this script. I'm trying to download zip file from the
    web and store it to my local drive: It is printing info, but doesn't
    download any thing.

    Looks like you are a wee bit confused on what you are trying to do.

    import urllib
    import sys
    # Get a file-like object from the web and store it locally.
    url = ""
    f = urllib.urlopen(url)

    Here you open a link to the url

    # connect to the remote
    try:
    remote = urllib.urlopen(url)

    and here you open another to the same url - do you need 2?

    # Read from the object, storing the page's contents in 's' want
    to download parcels.zip.
    s = f.read(Parcels.zip)

    But now you are trying to read from the url, but you pass a variable
    name
    (Parcels.zip) which is probably not recognised? If it was a filename
    it would
    have quotres around it. But read() doesn't take filenames so thatsd
    not
    right either.

    read() will read the p[age contents of your url back as a string.

    If the page has a link to Parcels.zip you will have to find it and
    extract
    the url for the file - you will probably find it esier to use your
    browser
    to do this, with View->source if necessary

    Then use the actual url to the zip file instead of the html file
    above.

    Assuming I've understood what you are trying to do correctly.

    HTH,

    Alan G.

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.3 | | 2722 bytes | |

    Matt- Have you tried running this line-by-line in IDLE? I've done a script
    almost exactly the same as what you're doing ( I downloaded a .jpg file from
    a web-server ), and when I was trying to learn what commands did what, I
    quickly changed from trying to write a 'program' to running lines
    individually in IDLE so I could print out the results of each command, and
    to see exactly what lines 'work', which have syntax errors and which have
    logic errors.

    People often want a compiler which spits out executables, but I realized
    it's nice to be able to type a few lines of an algorithm into the
    interpreter and see the results, so I can see if I get the results I expect,
    then copy that and paste it into my 'program'.
    -Dave

    Now, for wxPython (a windowing superset of python), my routine was:

    WebImage = urllib2.urlopen("%s/jpg/fullsize.jpg" %netCamAddress)
    rawJpg = WebImage.read()
    photostream = cStringIStringI(rawJpg)
    photoJpg = wx.ImageFromStream(photostream)
    photoBmp = wxBitmapFromImage(photoJpg)
    liveVideo.SetBitmap(photoBmp)

    "Give a man a fish; you have fed him for today. Teach a man to fish; and
    you have fed him for a lifetime. Show a man what a fish looks like so he
    doesn't try to eat a tire he fishes out of the pond."

    So, let me show you what a "fish" looks like. I expect the first two lines
    are of use to you, where the bottom-four lines are specific to wxPython.
    Realize what is happening with the string in the first line? I'm replacing
    part of the web-address with the contents of the variable netCamAddress.
    Can you figure out how that is working? hint- you can try printing
    "%s/jpg/fullsize.jpg" %netCamAddress directly in IDLE after setting
    netCamAddress to a string value to see what the result is, so you know what
    urllib2 is opening

    After line two is executed, the contents of rawJpg is the .jpg file I
    downloaded. Do you know what to do next? Maybe open a file in binary mode,
    and write rawJpg' to the file then close it?

    Also, do you realize I'm using a different library for urlopen than you
    used? Do you know which one, and how to import it?

    Lastly, there are a couple ways you can specify your file in python.

    "D:\\Temp\\file"
    but I like using raw strings.
    r"d:\directory\file.ext"
    (I hate double-slashing. It's so visually messy, it makes it easy to miss
    things like commas I think)

    #SECTIN009200000000000000000
    Maybe sections 7.2 and 7.2.1 are relevent to the rest of your goals?
    -Dave

    Tutor maillist - Tutor (AT) python (DOT) org

Re: Download file from the web and store it locally.


max 4000 letters.
Your nickname that display:
In order to stop the spam: 2 + 1 =
QUESTION ON "Python"

EMSDN.COM