1

I'm trying to retrieve an image that is returned through a given URL using python, for example this one:

http://fundamentus.com.br/graficos3.php?codcvm=2453&tipo=108

I am trying to do this by using urllib retrieve method:

import urllib

urlStr = "http://fundamentus.com.br/graficos3.php?codcvm=2453&tipo=108"
filename = "image.png"
urllib.urlretrieve(urlStr,filename)

I already used this for other URLs, (such as http://chart.finance.yahoo.com/z?s=CMIG4.SA&t=9m), but for the first one it's not working.

Does anyone have an idea about how to make this for the given URL? Note: I'm using Python 2.7

3
  • When I visit this url by Chrome I see the image rendered in my browser. But when I retrieve this by urllib.urlretrieve method I got this message "Ativo nao encontrado" Commented Aug 18, 2016 at 0:15
  • fundamentus.com.br/graficos.php?papel=CMIG4&tipo=2 Commented Aug 18, 2016 at 0:17
  • Last image of this page, in the second tab Commented Aug 18, 2016 at 0:18

2 Answers 2

2

You need to use a session which you can do with requests:

import requests

with requests.Session() as s:
    s.get("http://fundamentus.com.br/graficos.php?papel=CMIG4&tipo=2")
    with open("out.png", "wb") as f:
       f.write(s.get("http://fundamentus.com.br/graficos3.php?codcvm=2453&tipo=108").content)

It works in your browser as you had visited the initial page where the image was so any necessary cookies were set.

Sign up to request clarification or add additional context in comments.

3 Comments

No prob, you're welcome. Not an overly obvious issue .
Never knew about requests before now. Might have to see if i will stop using urllib2 now. Another leisurely stroll through SO gives me a whole new module! THANKS
Yes, requests as the docs state is http for humans. It makes requests very simple doing most of the heavy lifting for you.
0

While more verbose than @PadraicCunningham response. This should also do the trick. I'd run into a similar problem (host would only support certain browsers), so i had to start using urllib2 instead of just urllib. pretty powerful and is a module which comes with python.

Basically, you capture all the information you need during your initial request, and add it to your next request and subsequent requests. The requests module seems to pretty much do all of this for you behind the scenes. If only I'd known about that all these years...

import urllib2

urlForCookie = 'http://fundamentus.com.br/graficos.php?papel=CMIG4&tipo=2'
urlForImage = 'http://fundamentus.com.br/graficos3.php?codcvm=2453&tipo=108'

initialRequest = urllib2.Request(urlForCookie)
siteCookie = urllib2.urlopen(req1).headers.get('Set-Cookie')

imageReq = urllib2.Request(urlForImage)
imageReq.add_header('cookie', siteCookie)

with open("image2.pny",'w') as f:
        f.write(urllib2.urlopen(req2).read())
        f.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.