Easily expand shortened URLs for Python

The expanded_url returned from the Twitter API is completely unreliable, so deploy it on your own.

It's very easy to do, just use urllib2.

expand_url.py


import urllib2

short_url = 'http://t.co/MmQomeme8u'
expand_url = urllib2.urlopen(short_url).geturl()

print expand_url
# python expand_url.py
http://www.huffingtonpost.jp/tabirai-okinawa/-_285_b_6483010.html?ncid=tweetlnkjphpmg00000001

However, this method is not all-purpose. It seems that it does not support redirect using JavaScript.

urllib2 geturl() does not work for some url redirects http://stackoverflow.com/questions/14319771/urllib2-geturl-does-not-work-for-some-url-redirects

Recommended Posts

Easily expand shortened URLs for Python
A tool for easily entering Python code
2016-10-30 else for Python3> for:
python [for myself]
About Python for loops
Python basics ② for statement
About Python, for ~ (range)
Refactoring tools for Python
python for android Toolchain
Easily beep with python
OpenCV for Python beginners
Install Python (for Windows)
[Python] for statement error
Python environment for projects
Let's use python janome easily
Easily format JSON in Python
About Fabric's support for Python 3
Easily serverless with Python with chalice
Python list, for statement, dictionary
Python for Data Analysis Chapter 4
Modern Python for intermediate users
Learning flow for Python beginners
BigQuery integration for Python users
Python learning plan for AI learning
Set Up for Mac (Python)
Search for strings in Python
OpenCV3 installation for Python3 @macOS
Python code memo for yourself
[Python] xmp tag for photos
Techniques for sorting in Python
pp4 (python power for anything)
Python3 environment construction (for beginners)
Roadmap for publishing Python packages
Python #function 2 for super beginners
Python template for Codeforces-manual test-
3 months note for starting Python
Qt for Python app self-update
100 Pandas knocks for Python beginners
Checkio's recommendation for learning Python
Keyword arguments for Python functions
[For organizing] Python development environment
[Python] Sample code for Python grammar
Python for super beginners Python #functions 1
[Python / PyQ] 4. list, for statement
Simple HTTP Server for python
[Python + Selenium] Tips for scraping
Python #list for super beginners
~ Tips for beginners to Python ③ ~
Extract only Python for preprocessing
Indentation formatting for python scripts
Introduction to Python For, While
About "for _ in range ():" in python
tesseract-OCR for Python [Japanese version]
[Python] Iterative processing (for, while)
Python for Data Analysis Chapter 3
Install dlib for Python (Windows)
Created a library for python that can easily handle morpheme division