Find where a t.co link goes to

2019-01-11 01:06发布

Given a t.co link, how can I find see where the link resolves? For example, if I have t.co/foo, I want a function or process that returns domain.com/bar.

9条回答
乱世女痞
2楼-- · 2019-01-11 01:28

Another Python solution, this time relying on the requests module instead of urllib2 (and all the rest of those libraries):

#!/usr/bin/env python

import requests

shorturl = raw_input("Enter the shortened URL in its entirety: ")
r = requests.get(shorturl)

print("""
The shortened URL forwards to:

    %s
""" % r.url)
查看更多
叼着烟拽天下
3楼-- · 2019-01-11 01:30

Here is an R solution, ported from other answers in this thread, and from example() code of the RCurl Package:

unshorten_url <- function(uri){
        require(RCurl)
        if(RCurl::url.exists(uri)){
                # listCurlOptions()
                opts <- list(
                        followlocation = TRUE,  # resolve redirects
                        ssl.verifyhost = FALSE, # suppress certain SSL errors
                        ssl.verifypeer = FALSE, 
                        nobody = TRUE, # perform HEAD request
                        verbose = FALSE
                );
                curlhandle = getCurlHandle(.opts = opts)
                getURL(uri, curl = curlhandle)
                info <- getCurlInfo(curlhandle)
                rm(curlhandle)  # release the curlhandle!
                info$effective.url
        } else {
                # just return the url as-is
                uri
        }
}
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-11 01:31

You could give unshorten.me a go. It has an API.

JSON:

http://api.unshort.me/?r=http://theshorturl.ly/28292&t=json

Would give you:

{
   "requestedURL":"http://theshorturl.ly/28292",
   "success":"true",
   "resolvedURL":"http://thefullurl.com/a-webiste/what-a-long-url"
}
查看更多
【Aperson】
5楼-- · 2019-01-11 01:33

You may try this Java code. Such is a code use the HttpURLConnection owned by java. : http://www.srccodes.com/p/article/37/expand-shortened-link-using-java?fb_action_ids=1544985322486585&fb_action_types=og.likes

How this URL Expander will work? Make HttpURLConnection to the shortened url (say http://goo.gl/WT6eFw).

Extract the value of HTTP header field "Location". And this value is nothing but the expanded or actual destination URL.

Close the connection.

查看更多
Juvenile、少年°
6楼-- · 2019-01-11 01:35

Here is a Python solution.

import urllib2

class HeadRequest(urllib2.Request):
    def get_method(self): return "HEAD"

def get_real(url):
    res = urllib2.urlopen(HeadRequest(url))
    return res.geturl()

Tested with an actual twitter t.co link:

url = "http://t.co/yla4TZys"
expanded = get_real(url)

expanded = http://twitter.com/shanselman/status/276958062156320768/photo/1

Wrap it up with a try-except and you are good to go.

查看更多
唯我独甜
7楼-- · 2019-01-11 01:37

curl -s -o /dev/null --head -w "%{url_effective}\n" -L "https://t.co/6e7LFNBv"

  • --head or -I only downloads HTTP headers
  • -w or --write-out prints the specified string after the output
  • -L or --location follows location headers
查看更多
登录 后发表回答