0

I'm on Python 3.6. How I can extract part of URL so I can use it as variable. For example, the URL is http://example.com/comp/project.sec and I would like to get the project part without .sec as variable.

How can I achieve that?

0

3 Answers 3

2
url = "http://example.com/comp/project.sec"
project = url.split("/")[-1].split(".")[0]
Sign up to request clarification or add additional context in comments.

3 Comments

You can edit your answer to change that :)
@ViG thanks, bro =^_^=
2

Here's an alternative, which removes split requirement.

from os.path import splitext, basename

splitext(basename('http://example.com/comp/project.sec'))[0]

2 Comments

os.path.basename on a URL?
This is much better than my answer
0
import urllib
urllib.parse.urlparse("http://example.com/comp/project.sec").path.split("/")[-1].split(".")[-1]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.