Search code examples
pythonpython-webbrowser

How to check web browser URL | Python |


Is their any way to check particular URL in web browser

I tried in this below format as well

print(webbrowser.get('windows-default').open_new_tab('https://facebook.com) and the output is True but also open the url.

Code :

import webbrowser

urls = ['facebook.com','https://instagram.com','http://twitter.com']
for i in urls:
    if webbrowser.get('windows-default').open_new_tab(i) == False:
        webbrowser.get('windows-default').open_new_tab('https://'+i)
    elif webbrowser.get('windows-default').open_new_tab('https://'+i) == False:
        webbrowser.get('windows-default').open_new_tab('http://'+i)

Solution

  • You can the validators! package:

    >>> import validators
    >>> validators.url("http://google.com")
    True
    >>> validators.url("http://google")
    ValidationFailure(func=url, args={'value': 'http://google', 'require_tld': True})
    >>> if not validators.url("http://google"):
    ...     print "not valid"
    ... 
    not valid
    >>>