8
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

短縮URLをお手軽に展開する for Python

Last updated at Posted at 2015-01-17

Twitter APIから帰ってくるexpanded_urlはまったくもって当てにならないので,自力で展開する.

やること自体は非常に簡単で,単に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

ただ,この方法も万能ではない.JavaScriptを用いたリダイレクトには対応していないっぽい.

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

8
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?