How to Get a Canonical URL in Python
Canonical URLs are a key component of an HTML document. These crucial links show the preferred version of a web page, and ensure users are receiving the most accurate impression of your information. If you need to obtain the rel canonical URL of an HTML document, we have an easy-to-use API solution that can be used in Python to retrieve the link instantly.
We will start the process off by running this command to install the SDK:
pip install cloudmersive-convert-api-client
Next, we will create an instance the API client and call the function with the following code:
from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'# create an instance of the API class
api_instance = cloudmersive_convert_api_client.EditHtmlApi(cloudmersive_convert_api_client.ApiClient(configuration))
input_file = '/path/to/inputfile' # file | Optional: Input file to perform the operation on. (optional)
input_file_url = 'input_file_url_example' # str | Optional: URL of a file to operate on as input. (optional)try:
# Gets the rel canonical URL for the HTML document
api_response = api_instance.edit_html_html_get_rel_canonical(input_file=input_file, input_file_url=input_file_url)
pprint(api_response)
except ApiException as e:
print("Exception when calling EditHtmlApi->edit_html_html_get_rel_canonical: %s\n" % e)
And that’s all you need to do! The canonical URL of the input HTML document will be promptly delivered as a result.