user2087765
user2087765

Reputation: 43

Convert short URL

Two day old novice to Python (and programming) so please be gentle.

I have about 1500 shortened URLs scraped from Twitter. They are all in the following format: http://t.co/...

Using this to expand the short URL:

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

The last two lines repeated about 1500 different times with different URLs.

It works well as long as the page to which the URL points to exists however when it doesn't there is an error message and it stops at that point. What do I add to the code so that it returns a "page not found" and continues on to the next URL and goes through the entire list without stopping.

Upvotes: 4

Views: 303

Answers (1)

mderk
mderk

Reputation: 794

Assuming you're using python 2 (python 3 has slightly other syntax for exception handling)

for url in urls:
    try:
        a = urllib2.urlopen(url)  
    except urllib2.HTTPError, e:
        print "Error", e
        continue

..... do something with a   

Upvotes: 2

Related Questions