Slicing URL with Python

后端 未结 10 1186
予麋鹿
予麋鹿 2020-12-15 01:17

I am working with a huge list of URL\'s. Just a quick question I have trying to slice a part of the URL out, see below:

http://www.domainname.com/page?CONTEN         


        
10条回答
  •  误落风尘
    2020-12-15 01:32

    This method isn't dependent on the position of the parameter within the url string. This could be refined, I'm sure, but it gets the point across.

    url = 'http://www.domainname.com/page?CONTENT_ITEM_ID=1234¶m2¶m3'
    parts = url.split('?')
    id = dict(i.split('=') for i in parts[1].split('&'))['CONTENT_ITEM_ID']
    new_url = parts[0] + '?CONTENT_ITEM_ID=' + id
    

提交回复
热议问题