Python - Split url into its components

浪子不回头ぞ 提交于 2020-01-24 03:55:40

问题


I have a huge list of urls that are all like this:

http://www.example.com/site/section1/VAR1/VAR2

Where VAR1 and VAR2 are the dynamic elements of the url. What I want to do is to extract from this url string only the VAR1. I've tried to use urlparse but the output look like this:

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')

回答1:


Alternatively, you can apply the split() method:

>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']



回答2:


You can remember this in general. Different sections of the url can be obtained using urlparse. Here you can obtain the path by urlparse(url).path and then obtain the desired variable by split() function

>>> from urlparse import urlparse
>>> url = 'http://www.example.com/site/section1/VAR1/VAR2' 
>>> urlparse(url)
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
>>> urlparse(url).path
'/site/section1/VAR1/VAR2'
>>> urlparse(url).path.split('/')[-2]
'VAR1'



回答3:


Check this one, It is quite efficient because it starts from end of the string with maxsplit option we can stop number of splits.

Finally you can use indexing to get the last two part of the url

>>> url.rsplit('/',2)[1:]
['VAR1', 'VAR2']



回答4:


I would simply try

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]


来源:https://stackoverflow.com/questions/31170220/python-split-url-into-its-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!