简体   繁体   English

使用 Python 从 URL 中提取唯一 ID

[英]extract Unique id from the URL using Python

I've a URL like this:我有一个这样的 URL:

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.我想在 url 的最后提取 id 你可以从上面的字符串中说出 x 部分来获得唯一的 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.我正在使用它,但它给出了错误。

Python's urllib.parse and pathlib builtin libraries can help here. Python 的urllib.parsepathlib内置库可以提供帮助。

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

To print the text Enterprise-Business-Planning-Analyst_3103928-1 you can split() with respect to the / character:要打印文本Enterprise-Business-Planning-Analyst_3103928-1 ,您可以针对/字符进行split()

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:要打印文本3103928 ,您可以将_字符替换为-并且您可以针对-字符进行split()

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

# 3103928

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM