Convert Short URL

A two day newbie in Python (and programming), so please be careful.

I have about 1,500 shortened URLs cleared of Twitter. They are all in the following format: http://t.co/ ...

Using this to deploy a short url:

import urllib2 a = urllib2.urlopen('http://t.co/..') print a.url 

The last two lines were repeated about 1,500 times with different URLs.

It works well as long as the page that the URL points to, however, when there is no error message in it, it stops at that point. What am I adding to the code so that it returns β€œpage not found” and goes to the next URL and goes through the whole list without stopping.

+4
source share
1 answer

Assuming you are using python 2 (python 3 has a slightly different syntax for handling exceptions)

 for url in urls: try: a = urllib2.urlopen(url) except urllib2.HTTPError, e: print "Error", e continue ..... do something with a 
+2
source

All Articles