user2909590
user2909590

Reputation: 155

Download a data URI using Python

I searched a bit but didn't get an answer for this. I was trying to download some image files from a website using Python. When the website html has the image's url, I can use something like

urllib.urlretrieve(pic_url, pic_name) 

But when it is a data URI of something like

<img src="data:...."> 

how can I download that image from the website?

Thank you in advance!

Upvotes: 1

Views: 1279

Answers (1)

Sean Perry
Sean Perry

Reputation: 3886

See here http://en.wikipedia.org/wiki/Data_URI_scheme the data is actually all there in the HTML. You just need some form of HTML parser to locate the tag, get the attribute for src= and then decode the data in whatever format it is in, usually base64.

Using the example from Wikipedia for the red dot png:

>>> import base64
>>> png_data = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
>>> with open('/tmp/red.png', 'w') as fp:
...     fp.write(base64.b64decode())
...

Now if you look at /tmp/red.png it is a small, red dot.

Consider something like BeautifulSoup for HTML handling.

Upvotes: 1

Related Questions