Python 中将基本 URL 与 另一个 URL 连接起来
使用 urllib.parse 模块中的 urljoin
方法将一个基本 URL 与另一个 URL 连接起来,例如 result = urljoin(base_url, path)
。 urljoin
方法通过将基本 URL 与另一个 URL 组合来构造完整(绝对)URL。
from urllib.parse import urljoin
base_url = 'https://example.com'
path = 'images/static/cat.jpg'
result = urljoin(base_url, path)
# 👇️ https://example.com/images/static/cat.jpg
print(result)
# 👇️ /global/images/static/dog.png
print(urljoin('/global/images/', 'static/dog.png'))
如果我们有多个 URL 组件,请在将它们传递给 urljoin()
方法之前使用 posixpath 模块加入它们。
import posixpath
from urllib.parse import urljoin
base_url = 'https://example.com'
path_1 = 'images'
path_2 = 'static'
path_3 = 'cat.jpg'
path = posixpath.join(path_1, path_2, path_3)
print(path) # 👉️ 'images/static/cat.jpg'
result = urljoin(base_url, path)
# 👇️ https://example.com/images/static/cat.jpg
print(result)
urllib.parse.urljoin
方法将一个基本 URL 和另一个 URL 作为参数,并通过组合它们来构造一个完整的(绝对)URL。
我们还可以使用 urljoin
方法加入 URL 路径组件。
from urllib.parse import urljoin
# 👇️ /global/images/static/dog.png
print(urljoin('/global/images/', 'static/dog.png'))
确保我们得到的输出是我们所期望的,因为 urljoin
方法在处理不以正斜杠 / 结尾的 URL 组件时可能会有点混乱。
这是一个例子。
from urllib.parse import urljoin
# 👇️ /global/static/dog.png
print(urljoin('/global/images', 'static/dog.png'))
请注意
,该方法在加入第二个组件之前从第一个组件中剥离了图像。
当第一个组件以正斜杠结尾时,该方法的行为与预期相同。
from urllib.parse import urljoin
# 👇️ /global/images/static/dog.png
print(urljoin('/global/images/', 'static/dog.png'))
如果第二个组件以正斜杠开头,我们可能还会注意到令人困惑的行为
from urllib.parse import urljoin
# 👇️ /static/dog.png
print(urljoin('/global/images', '/static/dog.png'))
当第二个组件以正斜杠开头时,假定它从根开始。
posixpath.join()
方法更具可预测性,也可用于连接 URL 路径组件。
import posixpath
# 👇️ /global/images/static/dog.png
print(posixpath.join('/global/images', 'static/dog.png'))
# 👇️ /global/images/static/dog.png
print(posixpath.join('/global/images/', 'static/dog.png'))
# 👇️ /static/dog.png
print(posixpath.join('/global/images', '/static/dog.png'))
posixpath.join
方法也可以传递超过 2 个路径。
import posixpath
# 👇️ /global/images/static/dog.png
print(posixpath.join('/global', 'images', 'static', 'dog.png'))
相关文章
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。
如何在 Pandas 中将 DataFrame 列转换为日期时间
发布时间:2024/04/20 浏览次数:101 分类:Python
-
本文介绍如何将 Pandas DataFrame 列转换为 Python 日期时间。