Python 中的 URL 短链接器

2024年8月29日 | 阅读 22 分钟

首先,让我们了解一下什么是 URL 短链接器。自互联网诞生以来,用户就一直依赖链接作为其主要的交通方式。URL 缩短是一种 Web 方法,可以显著缩短统一资源定位器 (URL),同时仍然指向所需的网站。这是通过使用重定向实现的,该重定向指向一个具有长 URL 的网页。它们允许用户通过单击 URL 在几秒钟内从一个网站导航到另一个网站。不幸的是,当添加跟踪参数和站点结构扩展以处理数百个页面时,链接通常会变得冗长而复杂。随着社交媒体的普及,长 URL 成为了一个问题。Twitter 过去将消息限制为最多 140 个字符,并且链接中的每个字符都会被计算。结果,一个长 URL 可能会占用您推文的全部内容。URL 缩短程序作为对共享这一难题的回应而演变。

URL 短链接器的优点

  • 在任何营销计划中,内容的重要性都怎么强调都不为过。您需要正确的 URL 来分发这些内容。URL 缩短器可确保相关消息触达您的目标受众,而不会占用社交媒体帖子中的过多空间。
  • 通过以下方式使人们更容易分享您的内容:客户可以通过简单且品牌化的 URL 了解有关您网站的所有信息。不再需要包含随机字母和数字组合的缩短 URL。
  • 使您的 URL 更具吸引力:从美学角度来看,较短的 URL 更具吸引力。虽然这可能看起来不重要,但较短的 URL 可能是鼓励某人单击您的链接的决定性因素。
  • 允许流量监控:Bit.ly 包含跟踪监视器,可随着时间的推移跟踪您的推文或帖子的分享活动。

当您将自己的自定义域连接到充当您创建的所有短链接基础的 URL 缩短器时,它被称为自定义或品牌 URL 缩短器。您可以选择自己的域名,而不是选择像 bit.ly 或 rebrand.ly 这样的通用域名。

链接短链接器执行的操作

  • 链接掩码:显而易见,缩短 URL 可以隐藏原始站点地址。这对消费者来说很糟糕,因为它允许垃圾邮件发送者和黑客隐藏危险链接。值得庆幸的是,Chrome 和其他浏览器中的安全功能(您顺便使用 Chrome 吗?我们不必再担心恶意链接掩码了。当您使用适当的链接掩码时,您可以采用一篇重要信息来分享,并将其简化,以便在您的社交信息中传达一个关键点。不要将链接掩码与链接伪装混淆,后者涉及向搜索引擎(也称为爬虫或机器人)提供虚假信息,同时将用户重定向到与伪造材料无关的 URL。这是一种黑帽 SEO,可能导致您的网站被从 Google 搜索结果中移除。
  • 链接缩短:分享一个简短且易记的 URL 比分享一个长 URL 要容易得多,特别是当后者包含随机整数时。在一次演示中,您会向一群观众大喊哪个 URL?当您用完名片时,在社交活动中,您会告诉人们哪个 URL?我个人使用 Derric. link/card。仅此而已。当您在论坛上讨论新想法时,您想在论坛上使用哪个 URL?在公开场合分享链接时,您应该使用链接缩短工具来帮助您使其看起来更好,并衡量点击了多少人。
  • 链接跟踪:缩短 URL 的最有说服力的论据是链接跟踪。作为数字营销人员和社交媒体经理,我们必须了解我们工作的成果。ClickMeter 公司拥有最棒的链接跟踪技术,可从点击到转化。您可以检查有多少真实的人点击了链接,有多少爬虫或蜘蛛点击了链接。这为您提供了您网站的真实访问者数量以及转化率的更真实图景。您可能会收到每日甚至地理细分(未显示)。还有大量其他值得探索的内容。
  • 链接跟踪:缩短 URL 的最有说服力的论据是链接跟踪。作为数字营销人员和社交媒体经理,我们必须了解我们工作的成果。我们非常肯定还需要每月更新我们的工作进展。如果我们无法展示我们的点击量——以及希望的转化量——我们的工作保障将非常有限。
  • 链接轮播:您是否曾经想过进行 A/B 测试但又 unsure 如何进行?高级链接功能允许您从链接内部将流量分成 50/50 — 或 75/25,或任何比例 — 从而执行小型登陆页面测试。现在,这对于广告来说不是必需的,因为您可以运行两个广告,而且还有其他工具,如 Optimizely,允许您在页面上而不是在链接内处理此问题,但有一些选项可用于您的链接进行流量拆分测试。您还可以进行竞赛,并将第一次点击重定向到一个“获胜”页面,而其余的点击则重定向到一个“再试一次”页面。
  • 链接交换或更改:尽管它很简单,但大多数链接缩短提供商不允许您这样做。您创建一个缩短的链接并在 12 个不同的社交媒体平台上分发它,但是链接指向的页面有一天被删除了。好运地找到并替换所有这些损坏的链接。不再是这样了。使用 Rebrandly,您拥有自己的链接。您可以选择更改目标 URL。您几个月前分享的 12 个链接的自动更新只需几秒钟。危机得以避免。

现在让我们看看用 Python 创建 URL 短链接器的代码,我们将需要一个 API 来实际执行缩短 URL 的操作,为此,我们将使用两个不同的 API 提供商,并编写两个不同的 Python 代码,它们都使用不同的 API 执行相同的 URL 缩短任务。我们将使用的两个 API 提供商是 Bitly URL Shortener 和 Cuttly URL Shortener。

让我们来看看 Cuttly URL Shortener 的代码。

另一个我们可以使用的很棒的 URL 短链接器是 Cuttly URL Shortener。它同样非常简单易用,尽管它需要 2-3 行额外的代码。但是,它不需要安装,总的来说更简单。首先,转到 Cuttly 并创建一个新帐户。然后,转到“编辑个人资料”并“生成新的 API 密钥”。我们将因此获得一组新的 API 密钥。请记下 API 密钥。因此,我们可以直接开始编程,而无需安装任何东西。尽管我们只需要一个简单的安装,但我相信我们大多数人已经有了。不过,如果您没有所需的库,安装命令是

现在,既然我们的必需库已经就位,让我们开始为其编写代码。

代码

输出

现在让我们看看已编写代码的输出,代码执行期间生成的所有输出都显示在下方,其中包含所有标准输出以及任何错误和异常。

please choose any one of the operations from the listed below the list of operations::
1. To enter the API key for the service account.
2. To enter the URL which you want to shorten.
3. To perform the operation of shortening the URL.
4. To print the URL which is shortened.
5. To exit from the code execution.
1
enter the API key of the service account::
UIWN56645G#YW
To keep on going with code execution, type [y] otherwise [n].
y
please choose any one of the operations from the listed below the list of operations::
1. To enter the API key for the service account.
2. To enter the URL which you want to shorten.
3. To perform the operation of shortening the URL.
4. To print the URL which is shortened.
5. To exit from the code execution.
2
Enter the URL that you want to shorten.
https://www.mywebsite.com/dashboard/stats/barchart
To keep on going with code execution, type [y] otherwise [n].
y
please choose any one of the operations from the listed below the list of operations::
1. To enter the API key for the service account.
2. To enter the URL which you want to shorten.
3. To perform the operation of shortening the URL.
4. To print the URL which is shortened.
5. To exit from the code execution.
3
URL shortened successfully.
To keep on going with code execution, type [y] otherwise [n].
y
please choose any one of the operations from the listed below the list of operations::
1. To enter the API key for the service account.
2. To enter the URL which you want to shorten.
3. To perform the operation of shortening the URL.
4. To print the URL which is shortened.
5. To exit from the code execution.
4
Shortened URL:https://cut.ly/i4tdxaE 
To keep on going with code execution, type [y] otherwise [n].
Y
please choose any one of the operations from the listed below the list of operations::
1. To enter the API key for the service account.
2. To enter the URL which you want to shorten.
3. To perform the operation of shortening the URL.
4. To print the URL which is shortened.
5. To exit from the code execution.
2
Enter the URL that you want to shorten.
https://website.
To keep on going with code execution, type [y] otherwise [n].
y
please choose any one of the operations from the listed below the list of operations::
1. To enter the API key for the service account.
2. To enter the URL which you want to shorten.
3. To perform the operation of shortening the URL.
4. To print the URL which is shortened.
5. To exit from the code execution.
3
URL shortened successfully.
To keep on going with code execution, type [y] otherwise [n].
y
please choose any one of the operations from the listed below the list of operations::
1. To enter the API key for the service account.
2. To enter the URL which you want to shorten.
3. To perform the operation of shortening the URL.
4. To print the URL which is shortened.
5. To exit from the code execution.
4
URL shortening got an exception None.


please choose any one of the operations from the listed below the list of operations::
1. To enter the API key for the service account.
2. To enter the URL which you want to shorten.
3. To perform the operation of shortening the URL.
4. To print the URL which is shortened.
5. To exit from the code execution.
5

在上述已编写的代码中,创建了上述已编写类的对象,并使用该对象调用该类内的所有上述已编写函数,用户有多种选择,例如输入服务帐户的 API 密钥,输入用户想要缩短的 URL,执行指定输入 URL 的实际缩短操作,一旦缩短操作成功执行,用户就可以打印已缩短的 URL,并且在最后一个选项中,用户可以通过选择最后一个也是最终选项来接受代码执行。从用户那里获取适当的输入,并根据用户为执行操作选择的选项提供适当的输出。

让我们来看看 Bitly URL Shortener 的代码。

Bitly URL Shortener 非常易于使用。您只需创建一个 Bitly 帐户。然后转到“Group Settings”并从下拉菜单中选择“Advanced Settings”。您可以在此处找到 API 选项。由于 API 不再受支持,请改用 OAuth。然后,创建 OAuth Token。复制令牌。Bitly 是一个 URL 缩短服务和链接管理平台,由 Bitly, Inc. 于 2008 年创立。它位于纽约市,是私营公司。Bitly 每月缩短 6 亿个 URL[4],用于社交媒体、短信和电子邮件。Bitly 通过收取对大量使用缩短 URL 的用户生成聚合数据的访问费用来赚钱。

代码

输出

Now let us have a look at the output of the written code, all the output generated by the code at the time of execution is shown below with all the standard outputs along with errors and exceptions if any.

please choose any one of the operations from the listed below the list of operations::
1. To enter the API token for the service account.
2. To enter the URL which you want to shorten.
3. To perform the operation of shortening the URL.
4. To print the URL which is shortened.
5. To enter the URL which you want to expand.
6. To perform the expansion operation of the URL.
7. To print the URL which is expanded.
8. To exit from the code execution.
1
enter the api token for the bitly service account::
CJX5E9OLE$H6
To keep on going with code execution, type [y] otherwise [n].
y
please choose any one of the operations from the listed below the list of operations::
1. To enter the API token for the service account.
2. To enter the URL which you want to shorten.
3. To perform the operation of shortening the URL.
4. To print the URL which is shortened.
5. To enter the URL which you want to expand.
6. To perform the expansion operation of the URL.
7. To print the URL which is expanded.
8. To exit from the code execution.
2
Enter the URL that you want to shorten.
https://www.mywebsite.com/dashboard/stats/barchart
To keep on going with code execution, type [y] otherwise [n].
y
please choose any one of the operations from the listed below the list of operations::
1. To enter the API token for the service account.
2. To enter the URL which you want to shorten.
3. To perform the operation of shortening the URL.
4. To print the URL which is shortened.
5. To enter the URL which you want to expand.
6. To perform the expansion operation of the URL.
7. To print the URL which is expanded.
8. To exit from the code execution.
3
URL shortened successfully.
To keep on going with code execution, type [y] otherwise [n].
y
please choose any one of the operations from the listed below the list of operations::
1. To enter the API token for the service account.
2. To enter the URL which you want to shorten.
3. To perform the operation of shortening the URL.
4. To print the URL which is shortened.
5. To enter the URL which you want to expand.
6. To perform the expansion operation of the URL.
7. To print the URL which is expanded.
8. To exit from the code execution.
4
Shortened URL:https://cut.ly/i4tdxaE 
To keep on going with code execution, type [y] otherwise [n].
y
please choose any one of the operations from the listed below the list of operations::
1. To enter the API token for the service account.
2. To enter the URL which you want to shorten.
3. To perform the operation of shortening the URL.
4. To print the URL which is shortened.
5. To enter the URL which you want to expand.
6. To perform the expansion operation of the URL.
7. To print the URL which is expanded.
8. To exit from the code execution.
5
Enter the URL that you want to expand.
https://cut.ly/i4tdxaE
To keep on going with code execution, type [y] otherwise [n].
y
please choose any one of the operations from the listed below the list of operations::
1. To enter the API token for the service account.
2. To enter the URL which you want to shorten.
3. To perform the operation of shortening the URL.
4. To print the URL which is shortened.
5. To enter the URL which you want to expand.
6. To perform the expansion operation of the URL.
7. To print the URL which is expanded.
8. To exit from the code execution.
6
URL expanded successfully.
To keep on going with code execution, type [y] otherwise [n].
y
please choose any one of the operations from the listed below the list of operations::
1. To enter the API token for the service account.
2. To enter the URL which you want to shorten.
3. To perform the operation of shortening the URL.
4. To print the URL which is shortened.
5. To enter the URL which you want to expand.
6. To perform the expansion operation of the URL.
7. To print the URL which is expanded.
8. To exit from the code execution.
7
Expanded URL:https://www.mywebsite.com/dashboard/stats/barchart.
To keep on going with code execution, type [y] otherwise [n].
y
please choose any one of the operations from the listed below the list of operations::
1. To enter the API token for the service account.
2. To enter the URL which you want to shorten.
3. To perform the operation of shortening the URL.
4. To print the URL which is shortened.
5. To enter the URL which you want to expand.
6. To perform the expansion operation of the URL.
7. To print the URL which is expanded.
8. To exit from the code execution.
8

在上述已编写的代码中,创建了上述已编写类的对象,并使用该对象调用该类内的所有上述已编写函数,用户有多种选择,例如输入服务帐户的 API 密钥,输入用户想要缩短的 URL,执行指定输入 URL 的实际缩短操作,一旦缩短操作成功执行,用户就可以打印已缩短的 URL,并且在最后一个选项中,用户可以通过选择最后一个也是最终选项来接受代码执行。从用户那里获取适当的输入,并根据用户为执行操作选择的选项提供适当的输出。

优点

  • 洞察:衡量您发布的每个链接的点击次数是 URL 短链接器最重要的功能之一。您可以根据您使用的压缩网络链接的服务,按人口统计信息(例如国家或性别)细分访问者。您还可以看到哪些社交媒体渠道或帖子产生了最多的兴趣。
  • 增加分享:您的观众会更容易在自己的帖子中点击、复制和分享缩短的链接,这将有助于您的内容获得有机的社交媒体曝光。
  • 专业性:您的受众可能已经习惯于看到以缩短链接形式分享的内容。如果您分享了一个未压缩的 URL,他们可能会察觉到,因为这种方法非常普遍。习惯于更整洁、缩短链接的受众可能会认为长格式 URL 显得草率或混乱。
  • 便利性:分享一个短链接比分享一个大而复杂的链接要容易得多。如果您无法实际点击您的链接,或者您正在一对一地营销您的网站,这一点尤其重要。无论是进行 PowerPoint 演示还是与潜在客户分享链接,URL 短链接器都可以帮助您生成一个简洁、易记的链接,该链接可以直接输入到智能手机或 Web 浏览器中。
  • 更强的参与度:您的受众更有可能记住缩短的 URL,特别是如果它与您的品牌相关。根据 Rebrandly 的说法,缩短的 URL 可以将参与度提高 39%。
  • 更高的点击率:在社交媒体上,人们习惯于看到缩短的 URL。因此,缩短您的链接将提高您的点击率。这是您可以对帖子策略进行的最简单的修改之一,以提高其有效性。还不信吗?自己看看分析数据!能够跟踪缩短 URL 的进度已经很有用了。

所以,在这篇文章中,我们了解了如何用 Python 实现一个 URL 短链接器。