Python论坛  - 讨论区

标题:[python-chinese] 要得到网址的目录名,改怎么写正则?

2007年09月04日 星期二 12:15

doudou doudou array.doudou在gmail.com
星期二 九月 4 12:15:46 HKT 2007

import urlparse
url='http://www.xxx.com/xxx/a.aspx'
protocol,host,path,param,query,fragment=urlparse.urlparse(url)
path=path.rsplit("/",1)[0]
new_url=urlparse.urlunparse((protocol,host,path,param,query,""))
print new_url

这个通用性比较强,我写爬虫时就是这么处理的。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年09月05日 星期三 11:02

骨头 ymirsoft在gmail.com
星期三 九月 5 11:02:58 HKT 2007

doudou的是正解……楼主提到的需求,其实把问题想得太简单了……例如:
http://www.test.com/test/test.php?path=bbb/ccc/ddd/eee
这样的路径……前面各位所用的方法就都要取错了……

楼主可以看一下urlparse模块里的url拆分算法……如果对性能有要求……可以自己进一步优化……
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20070905/440e4cb2/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号