Python 中的 URL 短链接器2024年8月29日 | 阅读 22 分钟 首先,让我们了解一下什么是 URL 短链接器。自互联网诞生以来,用户就一直依赖链接作为其主要的交通方式。URL 缩短是一种 Web 方法,可以显著缩短统一资源定位器 (URL),同时仍然指向所需的网站。这是通过使用重定向实现的,该重定向指向一个具有长 URL 的网页。它们允许用户通过单击 URL 在几秒钟内从一个网站导航到另一个网站。不幸的是,当添加跟踪参数和站点结构扩展以处理数百个页面时,链接通常会变得冗长而复杂。随着社交媒体的普及,长 URL 成为了一个问题。Twitter 过去将消息限制为最多 140 个字符,并且链接中的每个字符都会被计算。结果,一个长 URL 可能会占用您推文的全部内容。URL 缩短程序作为对共享这一难题的回应而演变。 URL 短链接器的优点
当您将自己的自定义域连接到充当您创建的所有短链接基础的 URL 缩短器时,它被称为自定义或品牌 URL 缩短器。您可以选择自己的域名,而不是选择像 bit.ly 或 rebrand.ly 这样的通用域名。 链接短链接器执行的操作
现在让我们看看用 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,并且在最后一个选项中,用户可以通过选择最后一个也是最终选项来接受代码执行。从用户那里获取适当的输入,并根据用户为执行操作选择的选项提供适当的输出。 优点
所以,在这篇文章中,我们了解了如何用 Python 实现一个 URL 短链接器。 |
我们请求您订阅我们的新闻通讯以获取最新更新。