Given an absolute url of a page, and a relative link found within that page, would there be a way to a) definitively reconstruct or b) best-effort reconstruct the absolute url of the relative link?
In my case, I'm reading an html file from a given url using beautiful soup, stripping out all the img tag sources, and trying to construct a list of absolute urls to the page images.
My Python function so far looks like:
function get_image_url(page_url,image_src):
from urlparse import urlparse
# parsed = urlparse('http://user:pass@NetLoc:80/path;parameters?query=argument#fragment')
parsed = urlparse(page_url)
url_base = parsed.netloc
url_path = parsed.path
if src.find('http') == 0:
# It's an absolute URL, do nothing.
pass
elif src.find('/') == 0:
# If it's a root URL, append it to the base URL:
src = 'http://' + url_base + src
else:
# If it's a relative URL, ?
NOTE: Don't need a Python answer, just the logic required.
very simple:
Use
urllib.parse.urljoin
to resolve a (possibly relative) URL against a base URL.But, the base URL of a web page isn't necessarily the same as the URL you fetched the document from, because HTML allows a page to specify its preferred base URL via the
BASE
element. The logic you need is as follows:(If you are parsing XHTML then in theory you ought to take into account the rather hairy XML Base specification instead. But you can probably get away without worrying about that, since no-one really uses XHTML.)