From ba9be2ee3fcbe25e3fcca56662e28d5f9ce87b51 Mon Sep 17 00:00:00 2001 From: Thomas Wade Date: Mon, 1 Oct 2018 10:03:37 +0930 Subject: [PATCH] Check if the user exists through their username element --- scrape.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/scrape.py b/scrape.py index c0f23be..b7f10e8 100755 --- a/scrape.py +++ b/scrape.py @@ -138,9 +138,14 @@ def Get_Basic_Info(user): # Grab their username # Use stripped_strings generator as workaround for users with premium badge - username = next(soup.find(class_='profileHeaderInfo__userName').stripped_strings) - user.username = username - print(' Username: ' + user.username) + username_elem = soup.find(class_='profileHeaderInfo__userName') + if username_elem: + username = next(username_elem.stripped_strings) + user.username = username + print(' Username: ' + user.username) + else: + print(' No username, cannot be a valid user') + return False # Grab their track count track_count_elem = soup.find('a', href='/' + user.url_username + '/tracks', class_='infoStats__statLink') @@ -166,6 +171,8 @@ def Get_Basic_Info(user): user.like_count = Human_Str_To_Int(str(like_count_elem.string.split(' ')[0])) print(' Likes: ' + str(user.like_count)) + return True + # Gets a list of fully-populated Track objects def Get_Tracks_Info(url, limit = 100): # Get the page and soup it @@ -384,8 +391,10 @@ if not args.dump: user.url_username = user_str print(' URL Username: ' + user.url_username) - # Get their basic info - Get_Basic_Info(user) + # Get their basic info and check if it was successful + if not Get_Basic_Info(user): + print(' Failed to get basic info, skipping') + continue # Get their tracks for track in Get_Tracks_Info('https://soundcloud.com/' + user.url_username + '/tracks', USER_MAX_TRACKS):