【问题标题】:Converting shortened URLs from different URL-shortening services back via Python通过 Python 将来自不同 URL 缩短服务的缩短 URL 转换回来
【发布时间】:2014-05-22 18:36:48
【问题描述】:

我想知道是否有一种方便的方法可以通过已经可用的 Python 库将缩短的 URL 转换回“本机”URL。例如,从缩短的 URL 列表中:

['some url from bitly', 'shortened url from twitter', ...]

【问题讨论】:

    标签: python url url-rewriting


    【解决方案1】:
    import requests
    r = requests.get("http://bit.ly/XXXX")
    print r.url
    

    r.url 将是内容所在的服务器返回的解析 url

    【讨论】:

    • 我会使用r = requests.get(url, allow_redirects=False),然后使用r.headers['location']。否则,requests跟随重定向并发出两个请求(至少)。
    【解决方案2】:

    使用标准库:

    import urllib2
    response = urllib2.urlopen('http://shorturl')
    response.geturl()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多