How can I remove the fragment identifier from a URL?

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.