1

On web pages, resources such as images, css and javascript are loaded by a client's web browser, when embedded with <img>, <link> and <script> tags respectively.

A resource URL can take different forms, it can be a full URL, for example:

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

It can be a relative path:

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

Or just a reference to the root

/images/animage.jpg

How could I create a function in python, that takes the URL of the page, and the URL of a resource on it and ensures that the full URL is returned?

For example:

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

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.