How to Syntactically Validate a URL in Python

A syntactic validation system for URLs is a fast and efficient way to remove annoying problems caused by invalid links. While seemingly simple, it can actually be quite tedious to set up, requiring a lot of different conditions to be manually set. So rather than dealing with all of that, let’s use an API that will allow you to be done with the task in a matter of minutes.

Start by setting up the client we will be using.

pip install cloudmersive-validate-api-client

Now for function call setup for domain_url_syntax_only:

from __future__ import print_functionimport timeimport cloudmersive_validate_api_clientfrom cloudmersive_validate_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_validate_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_validate_api_client.DomainApi(cloudmersive_validate_api_client.ApiClient(configuration))request = cloudmersive_validate_api_client.ValidateUrlRequestSyntaxOnly() # ValidateUrlRequestSyntaxOnly | Input URL informationtry:# Validate a URL syntacticallyapi_response = api_instance.domain_url_syntax_only(request)pprint(api_response)except ApiException as e:print("Exception when calling DomainApi->domain_url_syntax_only: %s\n" % e)

Done! Now just run it and get your results. If you want a more heavy-duty approach, you can use the url full validation function instead, which checks a variety of things including whether the URL’s endpoint is up and passes virus scanning.

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store