【问题标题】:Reverse Shortened URL to Get the Original URL Before the Shortening in Google Sheets反向缩短的 URL 以在 Google 表格中缩短之前获取原始 URL
【发布时间】:2019-10-10 13:49:11
【问题描述】:

我有一张表格,用于收集带有基于搜索查询的链接的推文。然而,Twitter 会为每条推文提供它们的短 URL 格式 (t.co),而不是共享的原始 URL。

有没有办法使用公式或谷歌代码来回溯短网址并获取最终目标网址?推文作者分享的原件?

     What i have        |       what i'm looking for
---------------------------------------------------------------------------
https://t​.co/dura1sUSxm | https://www.jpost.com/Breaking-News/Russia-Saudi-Arabia-plan-deals-for-2-bln-for-Putins-visit-to-Riyadh-604200
https://t​.co/Ayy7ww8dFX | https://www.washingtonpost.com/national-security/trump-says-little-as-his-gop-allies-condemn-turkeys-incursion-into-syria/2019/10/09/c46210f6-eaab-11e9-9306-47cb0324fd44_story.html
https://t​.co/WLj6PipXkC | https://www.newsweek.com/teacher-fired-refusing-sign-pro-document-1262083
https://t​.co/UoqiqfaHup | https://www.reuters.com/article/us-environment-waste-idUSKBN1WP1RE
https://t​.co/hO9swbmeeZ | https://www.washingtonpost.com/national-security/trump-says-little-as-his-gop-allies-condemn-turkeys-incursion-into-syria/2019/10/09/c46210f6-eaab-11e9-9306-47cb0324fd44_story.html
https://t​.co/Ve8ZpCp1s1 | https://www.reuters.com/article/us-environment-waste-idUSKBN1WP1RE

【问题讨论】:

    标签: google-apps-script google-sheets google-apps


    【解决方案1】:

    答案:

    是的,这可以使用UrlFetchAppfetch 方法,合并标题并读取Location 属性。

    方法:

    您可以使用UrlFetchApp 获取包含最终 URL 端点的目标标头。但是,将其置于循环中很重要,因为有时多个 URL 缩短服务会串联起来,因此您可能无法在一次获取后获得目标 URL。

    代码:

    function getLocation(url) {
      var fetched = UrlFetchApp.fetch(url, {
        followRedirects: false
      })
    
      if (fetched.getHeaders().Location !== undefined) {
        return getLocation(fetched.getHeaders().Location)
      }
      return url
    }
    

    【讨论】:

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