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
Active
Viewed 50 times
1 Answers
2
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

Shmack
- 1,933
- 2
- 18
- 23
-
thank you. The version with pyping should work on python2 but the other two options work. The last method brings a problem with it. With the links I get, I get the error message "ping: cannot resolve (the url): Unkown host – kaan46 Aug 18 '22 at 17:28
-
Use the new ping function I posted at the very bottom of the answer. I didn't like his ping function so I changed it a little. – Shmack Aug 18 '22 at 17:32
-
-
Ummm can you paste the exact output out into the original post - I may have to catch for a specific exception? Whats your OS? Whats the output from `import platform; platform.system()`? – Shmack Aug 18 '22 at 17:46