How can I unwrap t.co links with Python?

2019-01-25 22:10发布

I want to expand t.co short links into a full URL. How can I do that?

3条回答
forever°为你锁心
2楼-- · 2019-01-25 22:24

The easiest way in python2 is to use urllib2.urlopen():

tco_url = "http://t.co/whatever"
req = urllib2.urlopen(tco_url)
print req.url

will print the URL tco_url finally resolves to, after following all redirects.

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-25 22:27

Using requests, you could write:

>>> import requests
>>> print(requests.get("http://t.co/UVgwaemZ").url)
http://paper.li/vascoda/vascoda-partner
查看更多
放我归山
4楼-- · 2019-01-25 22:41

You should be able to use Twitter's API. Each tweet has a JSON representation; reading JSON from Python is straightforward.

One of the entities contained in the JSON is expanded_url, defined as "The fully resolved URL".

查看更多
登录 后发表回答