給定一個t.co鏈接,我怎麼能找到鏈接解決的地方?例如,如果我有t.co/foo,我想要一個返回domain.com/bar的函數或進程。找到一個t.co鏈接去哪裏
回答
你可以給unshorten.me一個去。它有一個API。
JSON:
http://api.unshort.me/?r=http://theshorturl.ly/28292&t=json
會給你:
{
"requestedURL":"http://theshorturl.ly/28292",
"success":"true",
"resolvedURL":"http://thefullurl.com/a-webiste/what-a-long-url"
}
如果你想在命令行做,捲曲的詳細選項來救援:
curl -v <url>
爲您提供HTTP答覆。對於t.co,它似乎給你一個HTTP/301的回覆(永久移動)。然後,有一個位置字段,指向縮短後的URL。
也curl -I
我會遠離您無法控制的外部API。這將簡單地向您的應用程序引入依賴項,這是潛在的失敗點,並且可能會花費您的資金使用。
CURL可以很好地做到這一點。以下是我在PHP做的:
function unshorten_url($url) {
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_FOLLOWLOCATION => TRUE, // the magic sauce
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYHOST => FALSE, // suppress certain SSL errors
CURLOPT_SSL_VERIFYPEER => FALSE,
));
curl_exec($ch);
return curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
}
我敢肯定,這可以適用於其他語言,甚至與UNIXy系統curl
命令腳本。
http://jonathonhill.net/2012-05-18/unshorten-urls-with-php-and-curl/
尼斯解決方案。我可以建議設置'CURLOPT_NOBODY => true',這樣就可以執行HEAD請求,而最終的資源實際上不會被提取? – MaxArt
這裏的等價命令行是什麼? –
不是一個混蛋,而是RTFM。 http://curl.haxx.se/docs/manpage.html –
curl -s -o /dev/null --head -w "%{url_effective}\n" -L "https://t.co/6e7LFNBv"
--head
或-I
僅下載的HTTP標頭-w
或--write-out
打印輸出後的指定字符串-L
或--location
如下位置標頭
這是一個Python解決方案。
import urllib2
class HeadRequest(urllib2.Request):
def get_method(self): return "HEAD"
def get_real(url):
res = urllib2.urlopen(HeadRequest(url))
return res.geturl()
測試與實際Twitter的t.co鏈接:
url = "http://t.co/yla4TZys"
expanded = get_real(url)
擴大= http://twitter.com/shanselman/status/276958062156320768/photo/1
與一試,只是把它包起來,你是好去。
它不適用於'http://t.co/OFlTpTzCqt'.throws HTTPError:HTTP錯誤303:HTTP服務器返回一個重定向錯誤會導致無限循環。 最近的30x錯誤信息是: 查看其他' – Moj
另一個Python溶液,此時依靠請求模塊代替的urllib2(和這些庫的所有其餘部分)上:
#!/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)
這裏是R溶液,從其他的答案移植在此線程,和從example()
RCurl包裝的代碼:
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
}
}
您可以試試這個Java代碼。這是一個使用Java擁有的HttpURLConnection的代碼。 : http://www.srccodes.com/p/article/37/expand-shortened-link-using-java?fb_action_ids=1544985322486585&fb_action_types=og.likes
這個URL擴展器如何工作? 將HttpURLConnection設爲縮短的網址(例如http://goo.gl/WT6eFw)。
提取HTTP頭字段「位置」的值。而這個值只不過是展開的或實際的目標網址。
關閉連接。
Twitter擴展了URL。假設您使用twitter API編碼爲json文件的單個推文 。
import json
urlInfo=[]
tweet=json.loads(tweet)
keyList=tweet.keys() # list of all posssible keys
tweet['entities'] # gives us values linked to entities
可以觀察到,有一個叫「網址」 鳴叫[「實體」] [「網址」]#給出映射到按鍵的URL
urlInfo=tweet['entities']['expanded_url'] # move it to a list
# iterating over the list.. gives shortened URL
# and expanded URL
for item in urlInfo:
if "url" and "expanded_url" in urlInfo.keys():
print(item["url"] + " "+item["expanded_url"])
- 1. 解開鏈接到bit.ly的t.co鏈接
- 2. 找到哪裏下一步()去
- 3. 鏈接/集成問題。我需要一個方向去哪裏?
- 4. 哪裏的鏈接去升級G1開發電話到1.6
- 5. 轉到頁面並在那裏打開鏈接,在一個去
- 6. php到java連接器,在哪裏找到好的一個
- 7. 去哪裏放哪裏?
- 8. 我在哪裏可以找到超級鏈接到symfony 2.7
- 9. java哪裏去了一年?
- 10. 哪裏有ViewEngines.DefaultEngine去了一個asp.net MVC?
- 11. 去哪裏WebRequest.RegisterPrefix()
- 12. 去哪裏
- 13. 去哪裏?
- 14. public.xml中的定義鏈接到哪裏?
- 15. 使用JavaScript解開t.co鏈接?
- 16. 如何解開與Python的t.co鏈接?
- 17. 獲取Tweets沒有t.co鏈接
- 18. 我在哪裏可以找到如何鏈接boost庫?
- 19. 在哪裏我可以找到替代red5plugin鏈接?
- 20. 我在哪裏找到我的rtmp服務器鏈接?
- 21. 哪裏可以找到鏈接設置面板
- 22. 科爾多瓦鏈接斷了。哪裏可以找到它?
- 23. 在哪裏可以找到jQuery庫鏈接?
- 24. VC++ 2008鏈接程序在哪裏找到庫?
- 25. 我的鏈接在哪裏?
- 26. Postgres的不知道去哪裏找
- 27. 你在哪裏找到JDBCProvider接口?
- 28. 你怎麼能找到System.loadLibrary()在哪裏找到一個庫?
- 29. 對於Resharper StyleCop - 去哪兒去哪裏
- 30. GMSPlacesClient.lookUpPhotosForPlaceID去了哪裏?
unshorten.me被關停值值他們的API,他們自己的成功的受害者。 (無法承擔每天600萬API調用的託管成本) – Shewfig
我爲此構建了一個小應用程序。退房www.find-t.co。 –
unshorten.me網站現在不見了。 –