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?
Just use split()
split()
>>> foo = "http://www.address.com/something#something" >>> foo = foo.split('#')[0] >>> foo 'http://www.address.com/something' >>>
use urlparse.urldefrag :
>>> urlparse.urldefrag("http://www.address.com/something#something") ('http://www.address.com/something', 'something')
In python 3, the urldefrag function urldefrag 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 the following:
>>> s="http://www.address.com/something#something" >>> s1=s.split("#")[0] >>> s1 'http://www.address.com/something'
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.
Source: https://habr.com/ru/post/889854/More articles:Problems installing the application through the Windows installer - installerTesting against hacking attempts - htmlJQuery BUG - a problem with mouse animation - jqueryHow to set the selected item in a QListWidget? - qtHow can I get around closing javascript? - javascriptSend JSON data to highcharts pie from asp.net MVC in C # - jsonSubversive: how to add to svn ignore - eclipseHow to avoid program exit after failure in connection with Boost Asio and C / C ++ - c ++NSMenuItem NSView does not stand out in the submenu - objective-cCreate a solutions folder using MSBuild - visual-studioAll Articles