strip string from url in Python -
input: url = http://127.0.0.1:8000/data/number/
http://127.0.0.1:8000/data/
consistent each page number.
output: number
instead of slicing url url[-4:-1]
, there better way it?
you can use combination of urlparse
, split
.
import urlparse url = "http://127.0.0.1:8000/data/number/" path = urlparse.urlparse(url).path val = path.split("/")[2] print val
this prints:
number
the output of urlparse
above url
parseresult(scheme='http', netloc='127.0.0.1:8000', path='/data/number/', params='', query='', fragment='')
we utilizing path
portion of tuple. split on /
, take second index.
Comments
Post a Comment