Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract a part of URL - python

I have an URL for example:

http://name.abc.wxyz:1234/Assts/asset.epx?id=F3F94D94-7232-4FA2-98EF-07sdfssfdsa3B5

From this Url I want to extract only 'asset.epx?id=F3F94D94-7232-4FA2-98EF-07sdfssfdsa3B5' how could i do that?

I am still learning regular expressions and I am not able to solve the above. Any suggestions would be appreciated.

like image 640
Sangamesh Hs Avatar asked Nov 29 '22 12:11

Sangamesh Hs


2 Answers

You can use urlparse assuming asset.epx is the same:

>>> import urlparse
>>> url = 'http://name.abc.wxyz:1234/Assts/asset.epx?id=F3F94D94-7232-4FA2-98EF-07sdfssfdsa3B5'
>>> res = urlparse.urlparse(url)
>>> print 'asset.epx?'+res.query
asset.epx?id=F3F94D94-7232-4FA2-98EF-07sdfssfdsa3B5

This is useful if you ever need other information from the url (You can print res to check out the other info you can get ;))

If you're using Python 3 though, you'll have to do from urllib.parse import urlparse.

like image 186
TerryA Avatar answered Dec 04 '22 04:12

TerryA


In this specific example splitting the string is enough:

url.split('/')[-1]

If you have a more complex URL I would recommend the yarl library for parsing it:

>>> import yarl  # pip install yarl
>>> url = yarl.URL('http://name.abc.wxyz:1234/Assts/asset.epx?id=F3F94D94-7232-4FA2-98EF-07sdfssfdsa3B5')
>>> url.path_qs
'/Assts/asset.epx?id=F3F94D94-7232-4FA2-98EF-07sdfssfdsa3B5'

You could also use the builtin urllib.parse library but I find that it gets in the way once you start doing complex things like:

>>> url.update_query(asd='foo').with_fragment('asd/foo/bar')
URL('http://name.abc.wxyz:1234/Assts/asset.epx?id=F3F94D94-7232-4FA2-98EF-07sdfssfdsa3B5&asd=foo#asd/foo/bar')
like image 21
Blender Avatar answered Dec 04 '22 05:12

Blender