I'm extracting hundreds of URLs from tweets. Most of these URLs are shortened by external services like bit.ly and I need to expand them for comparison purposes. Right now I'm using a method from a previous post: How can I unshorten a URL using python? My code is below:
r = requests.head(url)
if r.status_code / 100 == 3:
expanded_url = r.headers['Location']
else:
expanded_url = r.url
Most URL expansions take less than 3 seconds, but it adds up quickly for such a large set of URLs. Is there a faster way to do this?