how to check in python if array includes an url

Question:

is there a way in python to check, if an element of an array contains a url? The output recognizes for the different elements of the array if it is a hyperlink or not and highlights it.
the only way i can think of is to check if the array entry contains a "www". But also links like "google.com" should be recognized.

Asked By: kaan46

||

Answers:

Unless you want to do something like:

urls = ["www.google.com", "not a url", "etc.com", "lol"]
for url in urls:
    if "www" in url or "http" in url or "https" in url or ".com" in url:
    # the list could go on
        print(f"{url} is a url!")
    else:
        print(f"{url} is not a url :(")

you could probably do it a more efficient way by trying to ping the url – like so:

SO Post with pyping and usage: https://stackoverflow.com/a/33018614/3155240

pip install pyping

import pyping as p

urls = ["www.google.com", "not a url", "etc.com", "lol"]
for url in urls:
    r = p.ping(url)

    if r.ret_code == 0:
        print(f"{url} is a url!")
    else:
        print(f"{url} is not a url :(")

EDIT: I couldn’t get the pyping package to work, but with the same concept on the previously mentioned SO Post, you could use a ping function like so (link to answer – https://stackoverflow.com/a/32684938/3155240):

import platform    # For getting the operating system name
import subprocess  # For executing a shell command

def ping(host):
    """
    Returns True if host (str) responds to a ping request.
    Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
    """

    # Option for the number of packets as a function of
    param = '-n' if platform.system().lower()=='windows' else '-c'

    # Building the command. Ex: "ping -c 1 google.com"
    command = ['ping', param, '1', host]

    return subprocess.call(command) == 0

Working implementation of the above ping function:

urls = ["www.google.com", "not a url", "etc.com", "lol"]
for url in urls:
    r = ping(url)

    if r:
        print(f"{url} is a url!")
    else:
        print(f"{url} is not a url :(")

EDIT: I didn’t like his ping function – here’s mine.

import subprocess
import platform

def ping(url):
    param = "-n" if platform.system().lower() == "windows" else "-c"
    command = ["ping", param, "1", url]
    try:
        res = subprocess.check_output(command)
    except subprocess.CalledProcessError:
        return False
    else:
        return True
Answered By: Shmack
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.