Simple instructions on how to programmatically download an image from a web page?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Y2KYZFR1

    Simple instructions on how to programmatically download an image from a web page?

    I have search the group and not found a working example on how to
    simply download a .gif from a url.

    something as simple as
    http://groups.google.com/images/threadview_logo.gif should be pretty
    easy, guess not?

    I don't need to know about using a browser, I need to know how to do
    it programmaticall y from Python.

    Anyone know how to get it done?
  • Matt Goodall

    #2
    Re: Simple instructions on how to programmaticall y download an imagefrom a web page?

    Y2KYZFR1 wrote:
    [color=blue]
    >I have search the group and not found a working example on how to
    >simply download a .gif from a url.
    >
    >[/color]
    I guess you didn't think to search the documentation too ;-).
    [color=blue]
    >something as simple as
    >http://groups.google.com/images/threadview_logo.gif should be pretty
    >easy, guess not?
    >
    >[color=green][color=darkred]
    >>> import urllib
    >>> data = urllib.urlopen( imageUrl).read( )[/color][/color][/color]

    Cheers, Matt

    --
    Matt Goodall, Pollenation Internet Ltd
    w: http://www.pollenation.net
    e: matt@pollenatio n.net



    Comment

    • Dan Bishop

      #3
      Re: Simple instructions on how to programmaticall y download an image from a web page?

      jarrodhroberson @yahoo.com (Y2KYZFR1) wrote in message news:<c718a6cf. 0311262300.1b62 e6c0@posting.go ogle.com>...[color=blue]
      > I have search the group and not found a working example on how to
      > simply download a .gif from a url.
      >
      > something as simple as
      > http://groups.google.com/images/threadview_logo.gif should be pretty
      > easy, guess not?
      >
      > I don't need to know about using a browser, I need to know how to do
      > it programmaticall y from Python.
      >
      > Anyone know how to get it done?[/color]

      Try the urllib.urlopen function.

      Comment

      • Bengt Richter

        #4
        Re: Simple instructions on how to programmaticall y download an image from a web page?

        On 26 Nov 2003 23:00:54 -0800, jarrodhroberson @yahoo.com (Y2KYZFR1) wrote:
        [color=blue]
        >I have search the group and not found a working example on how to
        >simply download a .gif from a url.
        >
        >something as simple as
        >http://groups.google.com/images/threadview_logo.gif should be pretty
        >easy, guess not?
        >
        >I don't need to know about using a browser, I need to know how to do
        >it programmaticall y from Python.
        >
        >Anyone know how to get it done?[/color]
        I just tried this interactively some example lines from httplib
        (the if 1: was just to execute the indented lines in one go)
        [color=blue][color=green][color=darkred]
        >>> import httplib
        >>> if 1:[/color][/color][/color]
        ... conn = httplib.HTTPCon nection("www.py thon.org")
        ... conn.request("G ET", "/pics/PythonPoweredSm all.gif")
        ... r1 = conn.getrespons e()
        ... print r1.status, r1.reason
        ... data1 = r1.read()
        ...
        200 OK[color=blue][color=green][color=darkred]
        >>> len(data1)[/color][/color][/color]
        361[color=blue][color=green][color=darkred]
        >>> data1[:8][/color][/color][/color]
        'GIF89a7\x00'[color=blue][color=green][color=darkred]
        >>> file('pypowgif. gif','wb').writ e(data1)
        >>> import os, webbrowser
        >>> webbrowser.open _new(os.path.ab spath('pypowgif .gif'))[/color][/color][/color]

        or using windows file association, also
        [color=blue][color=green][color=darkred]
        >>> os.system(os.pa th.abspath('pyp owgif.gif'))[/color][/color][/color]

        You should check errors etc., but the data apparently got through
        (not the logo you asked for though ;-)


        Regards,
        Bengt Richter

        Comment

        • Ville Vainio

          #5
          Re: Simple instructions on how to programmaticall y download an image from a web page?

          > On 26 Nov 2003 23:00:54 -0800, jarrodhroberson @yahoo.com (Y2KYZFR1) wrote:[color=blue]
          >[color=green]
          > >I have search the group and not found a working example on how to
          > >simply download a .gif from a url.[/color][/color]

          It's trivial with urllib.

          --
          Ville Vainio http://www.students.tut.fi/~vainio24

          Comment

          Working...