Home > Enterprise >  extract Unique id from the URL using Python
extract Unique id from the URL using Python

Time:01-29

I've a URL like this:

url = 'https://hp.wd5.myworkdayjobs.com/en-US/ExternalCareerSite/job/Enterprise-Business-Planning-Analyst_3103928-1'
x= 'Enterprise-Business-Planning-Analyst_3103928-1'

I want to extract id at the last of url you can say the x part from the above string to get the unique id.

Any help regarding this will be highly appreciated.

_parsed_url.path.split("/")[-1].split('-')[-1]

I am using this but it is giving error.

CodePudding user response:

Python's urllib.parse and pathlib builtin libraries can help here.

url = 'https://hp.wd5.myworkdayjobs.com/en-US/ExternalCareerSite/job/Enterprise-Business-Planning-Analyst_3103928-1'

from urllib.parse import urlparse
from pathlib import PurePath

x = PurePath(urlparse(url).path).name

print(x)
# Enterprise-Business-Planning-Analyst_3103928-1

CodePudding user response:

To print the text Enterprise-Business-Planning-Analyst_3103928-1 you can split() with respect to the / character:

url = 'https://hp.wd5.myworkdayjobs.com/en-US/ExternalCareerSite/job/Enterprise-Business-Planning-Analyst_3103928-1'
print(url.split("/")[-1])

# Enterprise-Business-Planning-Analyst_3103928-1

To print the text 3103928 you can replace the _ character with - and you can split() with respect to the - character:

url = 'https://hp.wd5.myworkdayjobs.com/en-US/ExternalCareerSite/job/Enterprise-Business-Planning-Analyst_3103928-1'
print(url.replace("_", "-").split("-")[-2])

# 3103928
  • Related