2012-02-23 2 views
1

웹 페이지에서 <img>, <link><script> 태그로 각각 포함 된 이미지, CSS 및 자바 스크립트와 같은 리소스는 클라이언트의 웹 브라우저에서로드됩니다.python 리소스 URL에서 전체 URL을 얻는 방법

자원의 URL은 예를 들어, 전체 URL이 될 수 있으며, 서로 다른 형태를 취할 수 : 루트에

images/animage.jpg 
../images/animage.jpg 

또는 단지 참조 :

http://cdn.mysite.com/images/animage.jpg 

그것은 상대 경로 일 수 있습니다

어떻게 페이지의 URL과 resour ce 그것에 전체 URL이 돌려 보내질다는 것을 보증 하는가? 예를 들어

는 :

def resource_url(page,resource): 
    ## if the resource is a full URL, return that 
    ## if not, use the page URL and the resource to return the full URL 
+1

당신은 urllib.parse.urljoin 방법 봤어? http://docs.python.org/release/3.1.3/library/urllib.parse.html – Peter

답변

1
from urlparse import urljoin 

def resource_url(page, resource): 
    if not resource.startswith(page): 
    # doesn't start with http://example.com 
    resource = urljoin(page, resource) 
    return resource 
관련 문제