python如何獲得一個url地址對應的跳轉后的最終網址
問題描述
在python中,如何獲取短鏈對應最終網址,現在有很多淘寶短鏈,我需要獲取短鏈跳到的最終網址,這個有什么好辦法嗎?有的是302 有的是直接在網頁用js進行跳轉,,這些如何獲取
問題解答
回答1:用selenium+phantonjs...
http://stackoverflow.com/ques...
#!/usr/bin/python2.7from twisted.internet import reactorfrom twisted.internet.defer import Deferred, DeferredList, DeferredLockfrom twisted.internet.defer import inlineCallbacksfrom twisted.web.client import Agent, HTTPConnectionPoolfrom twisted.web.http_headers import Headersfrom pprint import pprintfrom collections import defaultdictfrom urlparse import urlparsefrom random import randrangeimport fileinputpool = HTTPConnectionPool(reactor)pool.maxPersistentPerHost = 16agent = Agent(reactor, pool)locks = defaultdict(DeferredLock)locations = {}def getLock(url, simultaneous = 1): return locks[urlparse(url).netloc, randrange(simultaneous)]@inlineCallbacksdef getMapping(url): # Limit ourselves to 4 simultaneous connections per host # Tweak this as desired, but make sure that it no larger than # pool.maxPersistentPerHost lock = getLock(url,4) yield lock.acquire() try:resp = yield agent.request(’HEAD’, url)locations[url] = resp.headers.getRawHeaders(’location’,[None])[0] except Exception as e:locations[url] = str(e) finally: lock.release()
而且可以試試pip包
https://pypi.python.org/pypi/...
from urlunshort import resolveresolve('http://bit.ly/qlKaI') 結果 ’http://bitbucket.org/runeh/urlunshort/’
相關文章:
1. javascript - 關于定時器 與 防止連續點擊 問題2. javascript - 在IE中(9+)平時的樣式不生效,只有用開發者工具選中這個元素才生效,是什么原因?3. objective-c - ios百度地圖定位問題4. html - vue里面:src在IE(9-11)下不顯示圖片5. javascript - 關于這組數據如何實現 按字母列表分類展示 不改動數據結構6. javascript - 求助這種功能有什么好點的插件?7. javascript - ie11以下單擊打開不了file,雙擊可以。求解?8. html5 - rudy編譯sass的時候有中文報錯9. javascript - 寫移動端的頁面的時候,有不一快空白,是怎么回事?10. 微信開放平臺 - Android調用微信分享不顯示
