如何在 Python 中获取 URL 的基本部分
要获取 URL 的基本部分:
-
将 url 从
urllib.parse
模块传递给 urlparse 方法。 - 访问解析结果的 netloc 属性。
from urllib.parse import urlparse
my_url = 'https://example.com/images/wallpaper.jpg'
parsed = urlparse(my_url)
# 👇️ ParseResult(scheme='https', netloc='example.com', path='/images/wallpaper.jpg', params='', query='', fragment='')
print(parsed)
base = parsed.netloc
print(base) # 👉️ example.com
path = parsed.path
print(path) # 👉️ /images/wallpaper.jpg
with_path = base + '/'.join(path.split('/')[:-1])
print(with_path) # 👉️ example.com/images
print(path.split('/')) # 👉️ ['', 'images', 'wallpaper.jpg']
我们使用了 urllib.parse
模块中的 urlparse
方法。
urlparse
方法接受一个 URL 并将其解析为六个组件。
from urllib.parse import urlparse
my_url = 'https://example.com/images/wallpaper.jpg'
parsed = urlparse(my_url)
# 👇️ ParseResult(scheme='https', netloc='example.com', path='/images/wallpaper.jpg', params='', query='', fragment='')
print(parsed)
base = parsed.netloc
print(base) # 👉️ example.com
path = parsed.path
print(path) # 👉️ /images/wallpaper.jpg
解析结果的
netloc
属性返回基本 URL。
我们还可以访问其他属性,如 path
、query
等。
如果我们需要从结果中排除部分路径,请使用 str.rsplit()
或 str.split()
方法。
from urllib.parse import urlparse
my_url = 'https://example.com/images/wallpaper.jpg'
parsed = urlparse(my_url)
base = parsed.netloc
print(base) # 👉️ example.com
path = parsed.path
print(path) # 👉️ /images/wallpaper.jpg
with_path = base + '/'.join(path.rsplit('/', 1)[:-1])
print(with_path) # 👉️ example.com/images
print(path.rsplit('/', 1)) # 👉️ ['/images', 'wallpaper.jpg']
str.rsplit
方法使用提供的分隔符作为分隔符字符串返回字符串中的单词列表。
# 👇️ ['/images', 'wallpaper.jpg']
print('/images/wallpaper.jpg'.rsplit('/', 1))
# 👇️ ['', 'images', 'wallpaper.jpg']
print('/images/wallpaper.jpg'.rsplit('/'))
该方法采用以下 2 个参数:
- separator 在每次出现分隔符时将字符串拆分为子字符串
- maxsplit 最多完成最大拆分,最右边的拆分(可选)
除了从右侧拆分外,rsplit()
的行为类似于 split()
。
如果只想从右侧拆分一次,则可以将
maxsplit
参数设置为 1。
这是另一个嵌套更深的 URL 示例。
from urllib.parse import urlparse
my_url = 'https://example.com/images/nature/wallpaper.jpg'
parsed = urlparse(my_url)
base = parsed.netloc
print(base) # 👉️ example.com
path = parsed.path
print(path) # 👉️ /images/wallpaper.jpg
with_path = base + path.rsplit('/', 2)[0]
print(with_path) # 👉️ example.com/images
print(path.rsplit('/', 2)) # 👉️ ['/images', 'nature', 'wallpaper.jpg']
示例中的 URL 更深一层。
我们将 URL 从右侧拆分 2 次,并将 /images
路径添加到基本 URL。
相关文章
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 日期时间。