我有这个网址
www.testing.com/drive/rayon.productlist.pagination.topage/2?t:ac=3686975/4441810
我想要2
我在 python 2.7 中尝试过:
s = 'www.testing.com/drive/rayon.productlist.pagination.topage/2?t:ac=3686975/4441810'
s = s.substring(s.indexOf("topage/") + 1);
s = s.substring(0, s.indexOf("?"));
print s
我收到了这个错误
AttributeError: 'str' object has no attribute 'substring'
最佳答案
您应该使用urlparse
如果您需要 URL 的不同组成部分,请使用此函数。
>>> from urlparse import urlparse
>>> u = urlparse("www.testing.com/drive/rayon.productlist.pagination.topage/2?t:ac=3686975/4441810")
>>> u
ParseResult(scheme='', netloc='', path='www.testing.com/drive/rayon.productlist.pagination.topage/2', params='', query='t:ac=3686975/4441810', fragment='')
>>> u.path[-1]
'2'
>>>
关于python - 如何在Python中返回url路径的子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37069652/