DeltaWeb
DeltaWeb

Reputation: 381

How to exctract a part of a url in this case?

So I need a way to extract file names from public dropbox urls, without using the API, I've concluded that all sharing links in Dropbox are made like this :

dropbox.com/s/{random alphanemucric string}/{file name}

For example I have this link :

https://www.dropbox.com/s/sbyuft7zscqzgbs/checklist%281%29.pdf?dl=0

How can I extract the "checklist%281%29.pdf" part from this url, I don't have any idea how I would make my regex to extract this .

Upvotes: 2

Views: 51

Answers (2)

SparkAndShine
SparkAndShine

Reputation: 18017

Use urlparse.urlsplit,

import os
import urlparse 

url = 'https://www.dropbox.com/s/sbyuft7zscqzgbs/checklist%281%29.pdf?dl=0'

path = urlparse.urlsplit(url).path  # /s/sbyuft7zscqzgbs/checklist%281%29.pdf
filename = os.path.basename(path)   # checklist%281%29.pdf

Previous answer,

import os

url = 'https://www.dropbox.com/s/sbyuft7zscqzgbs/checklist%281%29.pdf?dl=0'
filename = os.path.basename(url).replace('?dl=0', '')

print(filename) # Output: checklist%281%29.pdf

Upvotes: 2

3kt
3kt

Reputation: 2553

You can do the following :

>>> name = 'https://www.dropbox.com/s/sbyuft7zscqzgbs/checklist%281%29.pdf?dl=0'
>>> name.split('/')[-1].split('?')[0]
'checklist%281%29.pdf'

The split("/")[-1] takes everything after the last slash, and the split('?')[0] removes parameters, if any.

Upvotes: 1

Related Questions