function is_absolute_url
Determines whether a given URL is absolute (contains a network location/domain) or relative.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/runtime/compat.py
44 - 45
simple
Purpose
This function checks if a URL string is absolute by parsing it and verifying the presence of a network location (netloc) component. An absolute URL includes a scheme and domain (e.g., 'https://example.com/path'), while a relative URL does not (e.g., '/path/to/resource'). This is commonly used in web scraping, URL validation, and link processing to distinguish between internal and external links or to determine if a URL needs to be joined with a base URL.
Source Code
def is_absolute_url(url):
return bool(urlparse(url).netloc)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
url |
- | - | positional_or_keyword |
Parameter Details
url: A string representing the URL to check. Can be any valid URL format including absolute URLs (e.g., 'https://example.com'), relative URLs (e.g., '/path'), or malformed URLs. The function expects a string input; passing None or non-string types may cause errors.
Return Value
Returns a boolean value: True if the URL is absolute (contains a network location component like 'example.com'), False if the URL is relative (no network location). The function uses bool() to convert the netloc attribute to a boolean, so any non-empty netloc string returns True, and an empty string returns False.
Required Imports
from urllib.parse import urlparse
Conditional/Optional Imports
These imports are only needed under specific conditions:
from urlparse import urlparse
Condition: Python 2.x compatibility - urlparse module location differs between Python 2 and 3
OptionalUsage Example
from urllib.parse import urlparse
def is_absolute_url(url):
return bool(urlparse(url).netloc)
# Example usage
print(is_absolute_url('https://www.example.com/path')) # True
print(is_absolute_url('http://example.com')) # True
print(is_absolute_url('/relative/path')) # False
print(is_absolute_url('path/to/file')) # False
print(is_absolute_url('//example.com/path')) # True (protocol-relative URL)
print(is_absolute_url('')) # False
Best Practices
- This function only checks for the presence of a network location (netloc), not whether the URL is valid or reachable
- Protocol-relative URLs (starting with '//') are considered absolute by this function
- The function does not validate URL syntax or check if the scheme is present; it only checks for netloc
- For Python 2/3 compatibility, ensure the correct urlparse import is used based on the Python version
- Consider adding input validation to handle None or non-string inputs if needed in production code
- This function treats URLs with only a scheme (e.g., 'http://') as relative since netloc would be empty
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_absolute_url 69.5% similar
-
function resolve_base_url 51.3% similar
-
function validate_sharepoint_url 48.9% similar
-
class WebPath 44.4% similar
-
class ResourcePath_v1 43.6% similar