2016-04-27 12 views
-1

Ich habe diese URL:Regulärer Ausdruck in Python mit urlparse

https://www.yoursite.com/drive/team-real-431/pepe-ozil-R323/anyway-jim-james-hi-bye-hi-321312/;jsessionid=DBDE454034B0EE325FC100112EF2E123.56AC29295781342F53AB242D03EE33 

i

DBDE454034B0EE325FC100112EF2E123.56AC29295781342F53AB242D03EE33 

ich versuchte

ulrJS = "https://www.yoursite.com/drive/team-real-431/pepe-ozil-R323/anyway-jim-james-hi-bye-hi-321312/;jsessionid=DBDE454034B0EE325FC100112EF2E123.56AC29295781342F53AB242D03EE33" 
ulrJS = ulrJS.split('/')[-1] 
+0

@Francesco Dank, herausgegeben ich meine Frage – parik

Antwort

1

haben möchten, können Sie verwenden urlparse:

>>> import urlparse 
>>> url = 'https://www.yoursite.com/drive/team-real-431/pepe-ozil-R323/anyway-jim-james-hi-bye-hi-321312/;jsessionid=DBDE454034B0EE325FC100112EF2E123.56AC29295781342F53AB242D03EE33' 
>>> url_parts = urlparse.urlparse(url) 
>>> jsessionid = dict(urlparse.parse_qsl(url_parts.params)).get('jsessionid') 
>>> print(jsessionid) 
DBDE454034B0EE325FC100112EF2E123.56AC29295781342F53AB242D03EE33 
+0

ich verwende Python 2.7, bekam ich diese Fehlermeldung:. Jsessionid = dict (urlparse.parse_qsl (url_parts.params)) erhalten ('jsessionid') Nameerror: name 'url_parts' ist nicht definiert – parik

+0

Ich aktualisierte die Antwort. – AKS

0
from urlparse import urlparse 
url ="https://www.yoursite.com/drive/team-real-431/pepe-ozil-R323/anyway-jim-james-hi-bye-hi-321312/;jsessionid=DBDE454034B0EE325FC100112EF2E123.56AC29295781342F53AB242D03EE33" 
u = urlparse(url) 
print u.params.split("=")[1] 
'DBDE454034B0EE325FC100112EF2E123.56AC29295781342F53AB242D03EE33' 
+0

Es ist besser, 'urlparse.parse_qsl' zu verwenden, falls Sie andere Parameter als' jsessionid' haben. – AKS