提取一部分 URL - python [英] Extract a part of URL - python
问题描述
我有一个网址,例如:
http://name.abc.wxyz:1234/Assts/asset.epx?id=F3F94D94-7232-4FA2-98EF-07sdfssfdsa3B5
从这个 URL 我只想提取 'asset.epx?id=F3F94D94-7232-4FA2-98EF-07sdfssfdsa3B5
' 我该怎么做?
我还在学习正则表达式,我无法解决上述问题.任何建议将不胜感激.
在这个具体的例子中,拆分字符串就足够了:
url.split('/')[-1]
如果您有更复杂的 URL,我建议您使用 yarl 库 来解析它:
<预><代码>>>>导入 yarl # pip install yarl>>>url = yarl.URL('http://name.abc.wxyz:1234/Assts/asset.epx?id=F3F94D94-7232-4FA2-98EF-07sdfssfdsa3B5')>>>url.path_qs'/Assts/asset.epx?id=F3F94D94-7232-4FA2-98EF-07sdfssfdsa3B5'你也可以使用内置的 urllib.parse
库,但我发现一旦你开始做一些复杂的事情,比如:
I have an URL for example:
http://name.abc.wxyz:1234/Assts/asset.epx?id=F3F94D94-7232-4FA2-98EF-07sdfssfdsa3B5
From this Url I want to extract only 'asset.epx?id=F3F94D94-7232-4FA2-98EF-07sdfssfdsa3B5
' how could i do that?
I am still learning regular expressions and I am not able to solve the above. Any suggestions would be appreciated.
In this specific example splitting the string is enough:
url.split('/')[-1]
If you have a more complex URL I would recommend the yarl library for parsing it:
>>> import yarl # pip install yarl
>>> url = yarl.URL('http://name.abc.wxyz:1234/Assts/asset.epx?id=F3F94D94-7232-4FA2-98EF-07sdfssfdsa3B5')
>>> url.path_qs
'/Assts/asset.epx?id=F3F94D94-7232-4FA2-98EF-07sdfssfdsa3B5'
You could also use the builtin urllib.parse
library but I find that it gets in the way once you start doing complex things like:
>>> url.update_query(asd='foo').with_fragment('asd/foo/bar')
URL('http://name.abc.wxyz:1234/Assts/asset.epx?id=F3F94D94-7232-4FA2-98EF-07sdfssfdsa3B5&asd=foo#asd/foo/bar')
这篇关于提取一部分 URL - python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!