Did you encounter Typeerror: only absolute urls are supported?
Well, when working with Python projects this is inevitable to experience.
So in this guide, we will know what this error means, its causes, and solutions to fix this error quickly.
What is Typeerror: only absolute urls are supported?
The ‘TypeError: Only Absolute URLs Are Supported’ is an error wherein occurs when using a relative URL instead of an absolute URL.
A relative URL is an URL relative to the current page only.
Meanwhile…
The absolute URL is a full URL that includes the protocol. For instance the domain name (www.myurl.com).
An example of how this error occurs is when we try to use a relative URL to point to an external website.
Wherein we should use relative URL when we point to another page on the same website.
How to fix Typeerror: only absolute urls are supported
When we are trying to use a relative URL in your Python code to access a resource.
Typically we can get TypeError: only absolute URLs are supported error.
Here is an example to fix the error.
- Identify the code where you are using the relative URL that is causing the error.
- Replace the relative URL with an absolute URL.
- Test your code to make sure that the error has been resolved.
Suppose you have the following Python code that retrieves the HTML content of a webpage using the requests library:
import requests
url = '/path/to/resource'
response = requests.get(url)
print(response.content)
When you run this code, you will get an error because you are using a relative URL.
To fix the error, you need to replace the relative URL with an absolute URL.
For example, if the resource you are trying to access is located at https://www.example.com/path/to/resource, you can modify your code as follows:
import requests
url = 'https://www.example.com/path/to/resource'
response = requests.get(url)
print(response.content)
Now, when you run the code, it should retrieve the HTML content of the webpage without any errors.
Output:
b'<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset="utf-8" />\n <meta http-equiv="Content-type" content="text/html; charset=utf-8" />\n <meta name="viewport" content="width=device-width, initial-scale=1" />\n <style type="text/css">\n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 2em;\n background-color: #fdfdff;\n border-radius: 0.5em;\n box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n @media (max-width: 700px) {\n div {\n margin: 0 auto;\n width: auto;\n }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</h1>\n <p>This domain is for use in illustrative examples in documents. You may use this\n domain in literature without prior coordination or asking for permission.</p>\n <p><a href="https://www.iana.org/domains/example">More information...</a></p>\n</div>\n</body>\n</html>\n'
An absolute URL is a web address that contains the complete path to a specific resource or webpage on the internet, including the protocol (such as HTTP or HTTPS), domain name, and the path to the resource.
An example of an absolute URL for the Google homepage would be “https://www.google.com/“.
Wherein “https://” is the protocol, “www.google.com” is the domain name, and the “/” at the end indicates the root directory of the website.
To get an absolute URL in Python, you can use the urllib.parse library, which provides various functions for parsing URLs.
The urlparse() function can be used to parse a given URL string and return a tuple that contains its different components, including the protocol, domain name, and path.
Conclusion
In conclusion, the error ‘TypeError: Only Absolute URLs Are Supported’ is an error wherein occurs when using a relative URL instead of an absolute URL.
If you are finding solutions to some errors you might encounter we also have Typeerror: nonetype object is not callable.
Thank you for reading!