Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove the fragment identifier from a URL?

Tags:

python

string

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

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

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

like image 412
xralf Avatar asked Jun 06 '11 09:06

xralf


People also ask

What is URL fragment ID?

The fragment identifier introduced by a hash mark # is the optional last part of a URL for a document. It is typically used to identify a portion of that document. The generic syntax is specified in RFC 3986. The hash-mark separator in URIs is not part of the fragment identifier.

How do I find the URL fragment?

var hash = url. substring(url. indexOf('#') + 1); alert(hash); Example 2: This example uses substring() method to display the fragment identifier.

Can a URL have multiple fragments?

Note that multiple text fragments can appear in one URL. The particular text fragments need to be separated by an ampersand character & .

How do I remove a URL from a form?

To request removal of a directory or site, click on the site in question, then go to Site configuration > Crawler access > Remove URL. If you enter the root of your site as the URL you want to remove, you'll be asked to confirm that you want to remove the entire site.


3 Answers

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

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

('http://www.address.com/something', 'something')
like image 73
kyrenia Avatar answered Sep 28 '22 18:09

kyrenia


For Python 2 use urlparse.urldefrag:

>>> urlparse.urldefrag("http://www.address.com/something#something")
('http://www.address.com/something', 'something')
like image 32
mouad Avatar answered Sep 28 '22 19:09

mouad


Just use split()

>>> foo = "http://www.address.com/something#something"
>>> foo = foo.split('#')[0]
>>> foo
'http://www.address.com/something'
>>>
like image 25
Mike Pennington Avatar answered Sep 28 '22 18:09

Mike Pennington