#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    2
    Rep Power
    0

    Problem with download use urllib


    Hi,

    I have a problem when use urllib to download something from web.

    the code looks like this:

    sock=urllib.urlopen("xxx.xxx.xxx/xxx.pdf")
    out=open("xxx.pdf")
    out.write(sock.read())
    out.close()

    the downloaded xxx.pdf is always little bigger than the original pdf if I download by click on the link.

    and xxx.pdf can not be opened by acrobat reader, anyone know why this is happening?

    thanks,
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I'd have to go with that the file hasn't been opened in write mode, or write-binary which you should probably be using here since as far as i know PDFs are binary files:

    Code:
    sock = urllib.urlopen('whatever/some.pdf')
    out = file('some.pdf', 'wb')
    out.write(sock.read())
    out.close()
    Note: you should use file() instead of open() in new programs .

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    2
    Rep Power
    0
    thanks a lot, that works.

IMN logo majestic logo threadwatch logo seochat tools logo