2012-02-23 63 views
1

在網頁上,當分別嵌入<img><link><script>標籤時,客戶端的Web瀏覽器會加載圖像,CSS和JavaScript等資源。如何從python中的資源URL獲取完整的URL

的資源URL可以採取不同的形式,也可以是一個完整的URL,例如:

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

它可以是一個相對路徑:

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

或者只是一個參考根

/images/animage.jpg 

我該如何在python中創建一個函數,該函數需要頁面的URL和資源的URL並且確保完整的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 2012-02-23 14:18:10

回答

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