Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to check in python if array includes an url

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.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading