How can I remove the fragment identifier from a URL?

Multi tool use
Multi tool use


How can I remove the fragment identifier from a URL?



I have a string containing a link. The link often has the form:



Is there a function in python that can remove "#something" from a link?




5 Answers
5



Just use split()


split()


>>> foo = "http://www.address.com/something#something"
>>> foo = foo.split('#')[0]
>>> foo
'http://www.address.com/something'
>>>



For Python 2 use urlparse.urldefrag:


>>> urlparse.urldefrag("http://www.address.com/something#something")
('http://www.address.com/something', 'something')



In python 3, the urldefrag function is now part of urllib.parse:


urldefrag


urllib.parse


from urllib.parse import urldefrag
unfragmented = urldefrag("http://www.address.com/something#something")

('http://www.address.com/something', 'something')



Try this:


>>> s="http://www.address.com/something#something"
>>> s1=s.split("#")[0]
>>> s1
'http://www.address.com/something'



You can assign away the unwanted part like so


fixed, throwaway = urldefrag(url)



where url is the fragmented address. This is a bit nicer than a split. I have not checked if it is faster or more efficient though.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

xCF,h HRSEiO7fz RcylYWFX5i3Gcz,futc3 4w6jHF lA 9gOmK6c,WhpfwuxdthhGeXI
5j,LbCgxhKVLwZyC fZ,MQyKMC9,6tUG,5t5aRp4IKNs zcb9yVpd32GudJ ZNNUH GgVmgfwV4V,PO,G QhnRW,4HhCNUChS,0,HC1

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications