binoubinks/ADSP_finalProjectBack
0
1import re2from urllib.parse import urlparse3 4# Function to extract all the features from the given URL5 6def extract_features(url):7 # Analyse de l'URL8 parsed_url = urlparse(url)9 domain = parsed_url.netloc10 domain_no_www = domain.replace('www.', '') if domain.startswith('www.') else domain11 domain_body_match = re.search(r'www\.(.+?)\.', domain)12 domain_body = domain_body_match.group(1) if domain_body_match else domain_no_www.split('.')[0]13 14 # Initialise caracteristics15 features = {}16 17 # URL 18 # features['URL'] = url19 20 # URLLength21 features['URLLength'] = len(url)22 23 # Domain24 features['Domain'] = domain25 26 # DomainLength27 features['DomainLength'] = len(domain)28 29 # TLD30 tld_match = re.search(r'\.[a-z]+$', domain_no_www)31 features['TLD'] = tld_match.group(0)[1:] if tld_match else ''32 33 # CharContinuationRate34 char_sequences = re.findall(r'[a-zA-Z]+', domain_body)35 total_chars = sum(len(seq) for seq in char_sequences)36 features['CharContinuationRate'] = total_chars / len(domain_body) if len(domain_body) > 0 else 037 38 # TLDLength39 features['TLDLength'] = len(features['TLD'])40 41 # NoOfSubDomain42 subdomains = domain_no_www.split('.')[:-1] 43 features['NoOfSubDomain'] = len(subdomains)44 45 # DegitRatioInURL46 digits = re.findall(r'[0-9]', url)47 features['DegitRatioInURL'] = len(digits) / len(url) if len(url) > 0 else 048 49 # SpacialCharRatioInURL50 special_chars = re.findall(r'[!@#$%^&*(),.?":{}|<>]', url)51 features['SpacialCharRatioInURL'] = len(special_chars) / len(url) if len(url) > 0 else 052 53 # IsHTTPS54 features['IsHTTPS'] = 1 if parsed_url.scheme == 'https' else 055 56 return features57 58# url_example = "https://www.southbankmosaics.com"59# url_example = "https://www.ooty.ind.in"60# features = extract_features(url_example)61# print(features)62# for key, value in features.items():63# print(f"{key}: {value}")