如何从网址获取特定的路径部分?例如,我想要一个对此进行操作的函数:
http://www.mydomain.com/hithere?image=2934
Run Code Online (Sandbox Code Playgroud)
并返回"hithere"
或对此进行操作:
http://www.mydomain.com/hithere/something/else
Run Code Online (Sandbox Code Playgroud)
并返回相同的东西("hithere")
我知道这可能会使用urllib或urllib2,但我无法从文档中找出如何仅获取路径的一部分.
Jos*_*Lee 38
使用urlparse提取URL的路径组件:
>>> import urlparse
>>> path = urlparse.urlparse('http://www.example.com/hithere/something/else').path
>>> path
'/hithere/something/else'
Run Code Online (Sandbox Code Playgroud)
使用os.path .split 将路径拆分为组件:
>>> import os.path
>>> os.path.split(path)
('/hithere/something', 'else')
Run Code Online (Sandbox Code Playgroud)
dirname和basename函数为您提供了两个分割; 也许在while循环中使用dirname:
>>> while os.path.dirname(path) != '/':
... path = os.path.dirname(path)
...
>>> path
'/hithere'
Run Code Online (Sandbox Code Playgroud)
Iwa*_*amp 17
最佳选择是在使用posixpath
URL的路径组件时使用该模块.os.path
当在基于POSIX和Windows NT的平台上使用时,此模块具有与POSIX路径相同的接口并始终如一地操作.
示例代码:
#!/usr/bin/env python3
import urllib.parse
import sys
import posixpath
import ntpath
import json
def path_parse( path_string, *, normalize = True, module = posixpath ):
result = []
if normalize:
tmp = module.normpath( path_string )
else:
tmp = path_string
while tmp != "/":
( tmp, item ) = module.split( tmp )
result.insert( 0, item )
return result
def dump_array( array ):
string = "[ "
for index, item in enumerate( array ):
if index > 0:
string += ", "
string += "\"{}\"".format( item )
string += " ]"
return string
def test_url( url, *, normalize = True, module = posixpath ):
url_parsed = urllib.parse.urlparse( url )
path_parsed = path_parse( urllib.parse.unquote( url_parsed.path ),
normalize=normalize, module=module )
sys.stdout.write( "{}\n --[n={},m={}]-->\n {}\n".format(
url, normalize, module.__name__, dump_array( path_parsed ) ) )
test_url( "http://eg.com/hithere/something/else" )
test_url( "http://eg.com/hithere/something/else/" )
test_url( "http://eg.com/hithere/something/else/", normalize = False )
test_url( "http://eg.com/hithere/../else" )
test_url( "http://eg.com/hithere/../else", normalize = False )
test_url( "http://eg.com/hithere/../../else" )
test_url( "http://eg.com/hithere/../../else", normalize = False )
test_url( "http://eg.com/hithere/something/./else" )
test_url( "http://eg.com/hithere/something/./else", normalize = False )
test_url( "http://eg.com/hithere/something/./else/./" )
test_url( "http://eg.com/hithere/something/./else/./", normalize = False )
test_url( "http://eg.com/see%5C/if%5C/this%5C/works", normalize = False )
test_url( "http://eg.com/see%5C/if%5C/this%5C/works", normalize = False,
module = ntpath )
Run Code Online (Sandbox Code Playgroud)
代码输出:
http://eg.com/hithere/something/else
--[n=True,m=posixpath]-->
[ "hithere", "something", "else" ]
http://eg.com/hithere/something/else/
--[n=True,m=posixpath]-->
[ "hithere", "something", "else" ]
http://eg.com/hithere/something/else/
--[n=False,m=posixpath]-->
[ "hithere", "something", "else", "" ]
http://eg.com/hithere/../else
--[n=True,m=posixpath]-->
[ "else" ]
http://eg.com/hithere/../else
--[n=False,m=posixpath]-->
[ "hithere", "..", "else" ]
http://eg.com/hithere/../../else
--[n=True,m=posixpath]-->
[ "else" ]
http://eg.com/hithere/../../else
--[n=False,m=posixpath]-->
[ "hithere", "..", "..", "else" ]
http://eg.com/hithere/something/./else
--[n=True,m=posixpath]-->
[ "hithere", "something", "else" ]
http://eg.com/hithere/something/./else
--[n=False,m=posixpath]-->
[ "hithere", "something", ".", "else" ]
http://eg.com/hithere/something/./else/./
--[n=True,m=posixpath]-->
[ "hithere", "something", "else" ]
http://eg.com/hithere/something/./else/./
--[n=False,m=posixpath]-->
[ "hithere", "something", ".", "else", ".", "" ]
http://eg.com/see%5C/if%5C/this%5C/works
--[n=False,m=posixpath]-->
[ "see\", "if\", "this\", "works" ]
http://eg.com/see%5C/if%5C/this%5C/works
--[n=False,m=ntpath]-->
[ "see", "if", "this", "works" ]
Run Code Online (Sandbox Code Playgroud)
笔记:
os.path
上ntpath
os.path
上posixpath
ntpath
不会\
正确处理反斜杠()(参见代码/输出中的最后两种情况) - 这posixpath
就是推荐的原因.urllib.parse.unquote
posixpath.normpath
/
未定义多个路径分隔符()的语义.然而,折叠多个相邻的路径分隔符(即它处理,并相同)posixpath
///
//
/
规范性参考文献:
Nav*_*vin 10
Python 3.4+解决方案:
from urllib.parse import unquote, urlparse
from pathlib import PurePosixPath
url = 'http://www.example.com/hithere/something/else'
PurePosixPath(
unquote(
urlparse(
url
).path
)
).parts[1]
# returns 'hithere' (the same for the URL with parameters)
# parts holds ('/', 'hithere', 'something', 'else')
# 0 1 2 3
Run Code Online (Sandbox Code Playgroud)
注意 Python3 import 已更改为from urllib.parse import urlparse
See documentation。下面是一个例子:
>>> from urllib.parse import urlparse
>>> url = 's3://bucket.test/my/file/directory'
>>> p = urlparse(url)
>>> p
ParseResult(scheme='s3', netloc='bucket.test', path='/my/file/directory', params='', query='', fragment='')
>>> p.scheme
's3'
>>> p.netloc
'bucket.test'
>>> p.path
'/my/file/directory'
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
40817 次 |
最近记录: |