From bb88bbeb24dfed168bf19d9244eb95c682b38d7a Mon Sep 17 00:00:00 2001 From: Thomas Wade Date: Sat, 29 Sep 2018 13:29:08 +0930 Subject: [PATCH] Convert human-readable numbers to ints --- scrape.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/scrape.py b/scrape.py index bdade95..d5bdee5 100755 --- a/scrape.py +++ b/scrape.py @@ -121,25 +121,25 @@ def Get_Basic_Info(user): # Grab their track count track_count_elem = soup.find('a', href='/' + user.url_username + '/tracks', class_='infoStats__statLink') if track_count_elem: - user.track_count = str(track_count_elem.div.string) + user.track_count = Human_Str_To_Int(str(track_count_elem.div.string)) print(' Tracks: ' + str(user.track_count)) # Grab their following count following_count_elem = soup.find('a', href='/' + user.url_username + '/following', class_='infoStats__statLink') if following_count_elem: - user.following_count = str(following_count_elem.div.string) + user.following_count = Human_Str_To_Int(str(following_count_elem.div.string)) print(' Following: ' + str(user.following_count)) # Grab their follower count follower_count_elem = soup.find('a', href='/' + user.url_username + '/followers', class_='infoStats__statLink') if follower_count_elem: - user.follower_count = str(follower_count_elem.div.string) + user.follower_count = Human_Str_To_Int(str(follower_count_elem.div.string)) print(' Followers: ' + str(user.follower_count)) # Grab their like count like_count_elem = soup.find(class_='sidebarHeader__actualTitle') if like_count_elem: - user.like_count = str(like_count_elem.string.split(' ')[0]) + user.like_count = Human_Str_To_Int(str(like_count_elem.string.split(' ')[0])) print(' Likes: ' + str(user.like_count)) # Gets a list of fully-populated Track objects @@ -212,19 +212,19 @@ def Get_Tracks_Info(url, limit = 100): for elem in play_comment_elems: num, unit = str(elem['title']).split(' ') if unit == 'plays': - track.plays = num + track.plays = Human_Str_To_Int(num) elif unit == 'comments': - track.comments = num + track.comments = Human_Str_To_Int(num) # Grab the like count like_elem = track_elem.find(class_='sc-button-like') if like_elem: - track.likes = str(like_elem.string.strip()) + track.likes = Human_Str_To_Int(str(like_elem.string.strip())) # Grab the repost count repost_elem = track_elem.find(class_='sc-button-repost') if repost_elem: - track.reposts = str(repost_elem.string.strip()) + track.reposts = Human_Str_To_Int(str(repost_elem.string.strip())) # Finally add the track to the dictionary tracks[track.url] = track @@ -276,6 +276,20 @@ def Get_Follows(url, limit = 100): return users.values() +def Human_Str_To_Int(string): + num = None + string = string.lower().replace(',', '') + if 'k' in string: + string = string.replace('k', '') + num = int(float(string) * 1000) + elif 'm' in string: + string = string.replace('m', '') + num = int(float(string) * 1000000) + else: + num = int(string) + + return num + ##### Scrape ################################################ user_dict = {}