Reputation: 3137
input: url = http://127.0.0.1:8000/data/number/
http://127.0.0.1:8000/data/
is consistent for each page number.
output: number
Instead of slicing the url url[-4:-1]
, Is there any better way to do it?
Upvotes: 1
Views: 117
Reputation: 50610
You can use a combination of urlparse
and 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
for the above URL is
ParseResult(scheme='http', netloc='127.0.0.1:8000', path='/data/number/', params='', query='', fragment='')
We are utilizing the path
portion of this tuple. We split it on /
and take the second index.
Upvotes: 2
Reputation: 2561
use urlparse module life would be easier
from urlparse import urlparse
urlparse('http://127.0.0.1:8000/data/number/').path.split('/')[2]
Upvotes: 2