Python 从动态url获取值

Python 从动态url获取值,python,regex,Python,Regex,我的url看起来像: http://www.example.com/blah/prod/4/x/blah.html 现在,如果此页面有子页面,它将如下所示: i、 e.在/prod/4之后,将有一个下划线,而不是另一个数字 同样,如果此页面有子页面,它将是: 我需要得到我放的所有文本???下面: /prod/???/x/blah.html 我该怎么做呢?比如这样。与模式prod/???/x/blah匹配的regexp,其中???是由数字和下划线组成的任意字符串: import re pat

我的url看起来像:

http://www.example.com/blah/prod/4/x/blah.html
现在,如果此页面有子页面,它将如下所示:

i、 e.在/prod/4之后,将有一个下划线,而不是另一个数字

同样,如果此页面有子页面,它将是:

我需要得到我放的所有文本???下面:

/prod/???/x/blah.html


我该怎么做呢?

比如这样。与模式prod/???/x/blah匹配的regexp,其中???是由数字和下划线组成的任意字符串:

import re
pattern = re.compile('prod/([\d_]+)/x/blah')
query   = "http://www.example.com/blah/prod/4_2343_234/x/blah.html"
result  = pattern.search(query).group(1)
print result
import urlparse
url = 'http://www.example.com/blah/prod/4_2343_234/x/blah.html'

urlparse.urlsplit(url).path.split('/')[3]
# returns '4_2343_234'