Python拆分url以查找图像名称和扩展名 [英] Python split url to find image name and extension

查看:457
本文介绍了Python拆分url以查找图像名称和扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用Python从特定url中提取文件名和扩展名的方法

I am looking for a way to extract a filename and extension from a particular url using Python

让我们说一个URL如下所示

lets say a URL looks as follows

picture_page = "http://distilleryimage2.instagram.com/da4ca3509a7b11e19e4a12313813ffc0_7.jpg"

我将如何获得以下内容。

How would I go about getting the following.

filename = "da4ca3509a7b11e19e4a12313813ffc0_7"
file_ext = ".jpg"


推荐答案

from urlparse import urlparse
from os.path import splitext, basename

picture_page = "http://distilleryimage2.instagram.com/da4ca3509a7b11e19e4a12313813ffc0_7.jpg"
disassembled = urlparse(picture_page)
filename, file_ext = splitext(basename(disassembled.path))

只有不利之处在于,您的文件名将包含以前的/您可以随时删除自己。

Only downside with this is that your filename will contain a preceding / which you can always remove yourself.

这篇关于Python拆分url以查找图像名称和扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆