How do I use Python to get the final URL from a list of shortened URLs? (Error 10061, ow.ly)

47 Views Asked by At

I have a list of some thousand URLs that I want to expand, to get the final URL they point to.

They are all Twitter links - so "https t.co xxxxxxxxxx" and some of them then become ow.ly links, so "https ow.ly xxxxxxxxxxx".

I am using the below code, with success, to get many of the final URLs:

import requests

results = []
num = 0

for item in list_of_urls:
    num += 1
    try: 
        r = requests.head(item,allow_redirects=True,timeout=2)   
        x = r.url
        print(f"{num}: {item} -> {x}")
        results.append(x)
    except:
        continue

However, all the ow.ly links time out when I try to expand them, giving me the error TCP error code 10061: No connection could be made because the target machine actively refused it.

Has anyone experienced this before? Is there anything I can do to bypass this error and get the final URLs?

0

There are 0 best solutions below