Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the true URL of a file on the web. (Python)

I notice that sometimes audio files on the internet have a "fake" URL.

http://garagaeband.com/3252243

And this will 302 to the real URL:

http://garageband.com/michael_jackson4.mp3

My question is...when supplied with the fake URL, how can you get the REAL URL from headers?

Currently, this is my code for reading the headers of a file. I don't know if this code will get me what I want to accomplish. How do I parse out the "real" URL From the response headers?

import httplib
conn = httplib.HTTPConnection(head)
conn.request("HEAD",tail)
res = conn.getresponse()

This has a 302 redirect: http://www.garageband.com/mp3cat/.UZCMYiqF7Kum/01_No_pierdas_la_fuente_del_gozo.mp3

like image 394
TIMEX Avatar asked Nov 17 '09 22:11

TIMEX


People also ask

How do you execute a URL in Python?

just open the python interpreter and type webbrowser. open('http://www.google.com') and see if it does what you want. yes. The result is same.


1 Answers

Use urllib.getUrl()

edit: Sorry, I haven't done this in a while:

import urllib
urllib.urlopen(url).geturl()

For example:

>>> f = urllib2.urlopen("http://tinyurl.com/oex2e")
>>> f.geturl()
'http://www.amazon.com/All-Creatures-Great-Small-Collection/dp/B00006G8FI'
>>> 
like image 185
Chris Lacasse Avatar answered Oct 11 '22 09:10

Chris Lacasse