"nba.com"인쇄 url
을 얻으려고 다시 "nba.com/sports을"= 얻을 URL을 공급하는 경우 당신은 당신이해야 할 일을 볼 수 있습니다 : 여기 후
>>> url = "http://www.nba.com/sports"
>>> url = url.split(".", 1)[1]
>>> print(url)
nba.com/sports
을, 우리가해야 할 일은 단지 /
에서 분할 분명하다.
>>> from urllib.parse import urlparse
>>> url = "http://www.nba.com/sports"
>>> urlparse(url)
ParseResult(scheme='http', netloc='www.nba.com', path='/sports', params='', query='', fragment='')
>>> urlparse(url).netloc
'www.nba.com'
을 그리고 당신은 다음 첫 번째 .
에서 모든 것을 제거 할 수 있습니다 : 너무 많이 복잡하게하지 마십시오 :
>>> url = url.split("/")[0]
>>> print(url)
nba.com
는 @ 마크가 코멘트에서 언급 한 바와 같이, 당신은 또한 urllib.urlparse
을 사용할 수 있습니다 필요하다면,하지만 당신이하고있는 것에 따라 당신은 할 필요가 없을 수도 있습니다.
파이썬 2를 사용하는 경우 모듈은 urlparse
입니다. 당신 nba.com
를 얻을 수
from urllib.parse import urlsplit
# On Py2, from urlparse import urlsplit
url = "http://www.nba.com/sports"
domain = urlsplit(url).hostname
# split off the last two components, then join them back together to make
# the second level domain
secondlevel = '.'.join(domain.rsplit('.', 2)[-2:])
print(secondlevel)
:
처음과 끝에서 'url'이 어떻게 보이는지, 그리고 결과로 원하는 것이 무엇인지 보여주세요. – TerryA
입력을 완료하기 전에 미안합니다. – bubba4399
을 다시 확인하십시오. 왜 urlparse를 사용하지 않습니까? – Mark