How to remove fragment id from url?

I have a line containing a link. A link often takes the form:

http://www.address.com/something#something

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

+6
source share
5 answers

Just use split()

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

use urlparse.urldefrag :

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

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

 from urllib.parse import urldefrag unfragmented = urldefrag("http://www.address.com/something#something") ('http://www.address.com/something', 'something') 
+3
source

Try the following:

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

You can assign an unwanted part this way

 fixed, throwaway = urldefrag(url) 

where url is the fragmented address. This is a little better than a split. I did not check if it works faster or more efficiently.

0
source

Source: https://habr.com/ru/post/889854/


All Articles