Slicing URL with Python

后端 未结 10 1108
予麋鹿
予麋鹿 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:37

    Parsin URL is never as simple I it seems to be, that's why there are the urlparse and urllib modules.

    E.G :

    import urllib
    url ="http://www.domainname.com/page?CONTENT_ITEM_ID=1234¶m2¶m3"
    query = urllib.splitquery(url)
    result = "?".join((query[0], query[1].split("&")[0]))
    print result
    'http://www.domainname.com/page?CONTENT_ITEM_ID=1234'
    

    This is still not 100 % reliable, but much more than splitting it yourself because there are a lot of valid url format that you and me don't know and discover one day in error logs.

提交回复
热议问题