如何在python中提取URL的特定部分?

时间:2019-01-24 21:35:36

标签: regex python-3.x url

我希望仅提取模式化URL的一部分:

https://<some_server>/server/rest/services/<that part I need>/<other data I do not need>/...

我只需要提取'rest / services /'之后的部分,第一部分可以更改,最后一部分可以更改,但是URL始终具有'rest / services /',我需要紧随其后的是“ /...

2 个答案:

答案 0 :(得分:0)

您可以做的是通过拆分将该字符串转换为列表,然后访问第n个部分。

m = 'https://<some_server>/server/rest/services/<that part I need>/<other data I do not need>'
a = m.split('/')
a[6] //This is the part that you want

答案 1 :(得分:0)

您可以尝试以下方法:

(?<=rest/services/) # look behind for this
[^/]+               # anything except a '/'

import re
rgxp = re.compile(r'(?<=rest/services/)[^/]+')
print re.findall(rgxp, text)