Python:获取 URL 路径部分 [英] Python: Get URL path sections
问题描述
如何从 url 获取特定的路径部分?例如,我想要一个对此进行操作的函数:
http://www.mydomain.com/hithere?image=2934
并返回到那里"
或对此进行操作:
http://www.mydomain.com/hithere/something/else
并返回相同的东西(到那里")
我知道这可能会使用 urllib 或 urllib2,但我无法从文档中弄清楚如何只获取路径的一部分.
使用 urlparse:
<预><代码>>>>导入 urlparse>>>path = urlparse.urlparse('http://www.example.com/hithere/something/else').path>>>小路'/这里/某物/其他'<预><代码>>>>导入 os.path>>>os.path.split(路径)('/hithere/something', '其他')dirname 和 basename 函数为您提供了拆分的两个部分;也许在 while 循环中使用 dirname:
<预><代码>>>>而 os.path.dirname(path) !='/':... path = os.path.dirname(path)...>>>小路'/你好呀'How do I get specific path sections from a url? For example, I want a function which operates on this:
http://www.mydomain.com/hithere?image=2934
and returns "hithere"
or operates on this:
http://www.mydomain.com/hithere/something/else
and returns the same thing ("hithere")
I know this will probably use urllib or urllib2 but I can't figure out from the docs how to get only a section of the path.
Extract the path component of the URL with urlparse:
>>> import urlparse
>>> path = urlparse.urlparse('http://www.example.com/hithere/something/else').path
>>> path
'/hithere/something/else'
Split the path into components with os.path.split:
>>> import os.path
>>> os.path.split(path)
('/hithere/something', 'else')
The dirname and basename functions give you the two pieces of the split; perhaps use dirname in a while loop:
>>> while os.path.dirname(path) != '/':
... path = os.path.dirname(path)
...
>>> path
'/hithere'
这篇关于Python:获取 URL 路径部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!