edsheeran edsheeran - 3 months ago 5
Python Question

How to get elements in a string after and until a specific point in Python?

I'm sure the title sounded a bit cryptic but I couldn't think of a better way to ask the question.

I have this code:

from bs4 import BeautifulSoup
from requests import get
import re

eune = 'http://www.lolking.net/leaderboards#/eune/1'

patt = re.compile("\$\.get\('/leaderboards/(\w+)/")
js = 'http://www.lolking.net/leaderboards/{}/eune/1.json'
url = get(eune)
soup = BeautifulSoup(url.text, 'lxml')
script = soup.find('script', text=re.compile("\$\.get\('/leaderboards/"))

val = patt.search(script.text).group(1)
json = get(js.format(val)).json()
print(str(json))


Basically I'm trying to make a username scraper and I'm getting the data in a json format which outputs like this:

{'data': [{'summoner_id': '42893043', 'tier': '6', 'global_ranking': '17', 'name': 'Sadastyczny', 'lks': '2961', 'wins': '128', 'region': 'eune', 'profile_icon_id': 944, 'division': '1', 'previous_ranking': '1', 'ranking': '1', 'tier_name': 'CHALLENGER', 'losses': '31', 'most_played_champions': [{'wins': '34', 'kills': '288', 'deaths': '131', 'creep_score': '7227', 'played': '39', 'champion_id': '236', 'assists': '238', 'losses': '5'}, {'wins': '24', 'kills': '204', 'deaths': '111', 'creep_score': '5454', 'played': '27', 'champion_id': '429', 'assists': '209', 'losses': '3'}, {'wins': '18', 'kills': '168', 'deaths': '103', 'creep_score': '4800', 'played': '26', 'champion_id': '81', 'assists': '155', 'losses': '8'}], 'league_points': '1217'}, {'summoner_id': '40385818', 'tier': '6', 'global_ranking': '40', 'name': 'Richor', 'lks': '2955', 'wins': '260', 'region': 'eune', 'profile_icon_id': 577, 'division': '1', 'previous_ranking': '2', 'ranking': '2', 'tier_name': 'CHALLENGER', 'losses': '187', 'most_played_champions': [{'wins': '150', 'kills': '1923', 'deaths': '1466', 'creep_score': '38820', 'played': '217', 'champion_id': '24', 'assists': '952', 'losses': '67'}, {'wins': '53', 'kills': '662', 'deaths': '584', 'creep_score': '16836', 'played': '90', 'champion_id': '67', 'assists': '501', 'losses': '37'}, {'wins': '15', 'kills': '150', 'deaths': '211', 'creep_score': '5372', 'played': '29', 'champion_id': '157', 'assists': '135', 'losses': '14'}], 'league_points': '1100'}, {'summoner_id': '21348510', 'tier': '6', 'global_ranking': '44', 'name': 'Azzapp', 'lks': '2954', 'wins': '415', 'region': 'eune', 'profile_icon_id': 588, 'division': '1', 'previous_ranking': '6', 'ranking': '3', 'tier_name': 'CHALLENGER', 'losses': '348', 'most_played_champions': [{'wins': '70', 'kills': '645', 'deaths': '576', 'creep_score': '9538', 'played': '122', 'champion_id': '161', 'assists': '1293', 'losses': '52'}, {'wins': '44', 'kills': '644', 'deaths': '385', 'creep_score': '14366', 'played': '80', 'champion_id': '202', 'assists': '762', 'losses': '36'}, {'wins': '41', 'kills': '473', 'deaths': '252', 'creep_score': '12970', 'played': '68', 'champion_id': '103', 'assists': '473', 'losses': '27'}], 'league_points': '1071'}, {'summoner_id': '24275559', 'tier': '6', 'global_ranking': '58', 'name': 'YanetGarcia1337', 'lks': '2953', 'wins': '152', 'region': 'eune', 'profile_icon_id': 1227, 'division': '1', 'previous_ranking': '3', 'ranking': '4', 'tier_name': 'CHALLENGER', 'losses': '47', 'most_played_champions': [{'wins': '59', 'kills': '199', 'deaths': '99', 'creep_score': '11927', 'played': '66', 'champion_id': '117', 'assists': '638', 'losses': '7'}, {'wins': '32', 'kills': '192', 'deaths': '99', 'creep_score': '8061', 'played': '43', 'champion_id': '48', 'assists': '345', 'losses': '11'}, {'wins': '20', 'kills': '165', 'deaths': '64', 'creep_score': '5584', 'played': '31', 'champion_id': '114', 'assists': '161', 'losses': '11'}], 'league_points': '1051'}, {'summoner_id': '31881620', 'tier': '6', 'global_ranking': '66', 'name': 'Rapisher', 'lks': '2952', 'wins': '158', 'region': 'eune', 'profile_icon_id': 518, 'division': '1', 'previous_ranking': '4', 'ranking': '5', 'tier_name': 'CHALLENGER', 'losses': '92', 'most_played_champions': [{'wins': '100', 'kills': '887', 'deaths': '597', 'creep_score': '26352', 'played': '137', 'champion_id': '429', 'assists': '905', 'losses': '37'}, {'wins': '22', 'kills': '10', 'deaths': '120', 'creep_score': '425', 'played': '36', 'champion_id': '40', 'assists': '533', 'losses': '14'}, {'wins': '15', 'kills': '162', 'deaths': '126', 'creep_score': '6134', 'played': '29', 'champion_id': '236', 'assists': '194', 'losses': '14'}], 'league_points': '1030'}, {'summoner_id': '36880627', 'tier': '6', 'global_ranking': '82', 'name': 'Kizuro', 'lks': '2951', 'wins': '158', 'region': 'eune', 'profile_icon_id': 909, 'division': '1', 'previous_ranking': '5', 'ranking': '6', 'tier_name': 'CHALLENGER', 'losses': '79', 'most_played_champions': [{'wins': '32', 'kills': '359', 'deaths': '190', 'creep_score': '1952', 'played': '49', 'champion_id': '76', 'assists': '377', 'losses': '17'}, {'wins': '29', 'kills': '297', 'deaths': '153', 'creep_score': '1266', 'played': '38', 'champion_id': '60', 'assists': '387', 'losses': '9'}, {'wins': '24', 'kills': '254', 'deaths': '163', 'creep_score': '1202', 'played': '37', 'champion_id': '64', 'assists': '308', 'losses': '13'}], 'league_points': '1010'}, {'summoner_id': '44064579', 'tier': '6', 'global_ranking': '124', 'name': 'Nataa', 'lks': '2946', 'wins': '278', 'region': 'eune', 'profile_icon_id': 2076, 'division': '1', 'previous_ranking': '7', 'ranking': '7', 'tier_name': 'CHALLENGER', 'losses': '213', 'most_played_champions': [{'wins': '52', 'kills': '651', 'deaths': '405', 'creep_score': '17094', 'played': '82', 'champion_id': '67', 'assists': '445', 'losses': '30'}, {'wins': '44', 'kills': '584', 'deaths': '354', 'creep_score': '16397', 'played': '74', 'champion_id': '236', 'assists': '463', 'losses': '30'}, {'wins': '25', 'kills': '349', 'deaths': '208', 'creep_score': '9768', 'played': '45', 'champion_id': '81', 'assists': '331', 'losses': '20'}], 'league_points': '920'}, {'summoner_id': '24504284', 'tier': '6', 'global_ranking': '165', 'name': 'Huunntér', 'lks': '2943', 'wins': '148', 'region': 'eune', 'profile_icon_id': 1234, 'division': '1', 'previous_ranking': '8', 'ranking': '8', 'tier_name': 'CHALLENGER', 'losses': '67', 'most_played_champions': [{'wins': '36', 'kills': '267', 'deaths': '263', 'creep_score': '2331', 'played': '54', 'champion_id': '60', 'assists': '449', 'losses': '18'}, {'wins': '33', 'kills': '270', 'deaths': '149', 'creep_score': '2880', 'played': '39', 'champion_id': '104', 'assists': '296', 'losses': '6'}, {'wins': '22', 'kills': '102', 'deaths': '76', 'creep_score': '865', 'played': '25', 'champion_id': '421', 'assists': '279', 'losses': '3'}], 'league_points': '858'}, {'summoner_id': '27565071', 'tier': '6', 'global_ranking': '209', 'name': 'Hades147', 'lks': '2941', 'wins': '243', 'region': 'eune', 'profile_icon_id': 588, 'division': '1', 'previous_ranking': '9', 'ranking': '9', 'tier_name': 'CHALLENGER', 'losses': '146', 'most_played_champions': [{'wins': '73', 'kills': '741', 'deaths': '261', 'creep_score': '5693', 'played': '94', 'champion_id': '102', 'assists': '675', 'losses': '21'}, {'wins': '27', 'kills': '199', 'deaths': '203', 'creep_score': '9370', 'played': '51', 'champion_id': '117', 'assists': '514', 'losses': '24'}, {'wins': '25', 'kills': '187', 'deaths': '179', 'creep_score': '2047', 'played': '42', 'champion_id': '79', 'assists': '420', 'losses': '17'}], 'league_points': '811'}, {'summoner_id': '35655567', 'tier': '6', 'global_ranking': '210', 'name': 'Yolo Que Warrior', 'lks': '2941', 'wins': '210', 'region': 'eune', 'profile_icon_id': 682, 'division': '1', 'previous_ranking': '11', 'ranking': '10', 'tier_name': 'CHALLENGER', 'losses': '148', 'most_played_champions': [{'wins': '46', 'kills': '445', 'deaths': '252', 'creep_score': '15061', 'played': '74', 'champion_id': '236', 'assists': '437', 'losses': '28'}, {'wins': '39', 'kills': '394', 'deaths': '179', 'creep_score': '3349', 'played': '68', 'champion_id': '76', 'assists': '593', 'losses': '29'}, {'wins': '22', 'kills': '267', 'deaths': '117', 'creep_score': '2683', 'played': '37', 'champion_id': '203', 'assists': '271', 'losses': '15'}], 'league_points': '811'}], 'status': True}


Now I need to find whatever is after
'name': '
and stop at
'
and store that in a list.
How would I do this?

Answer

If json is your dictionary, you can use a list comprehension:

username_list = [summoner['name'] for summoner in json['data']]

Output:

['Sadastyczny', 'Richor', 'Azzapp', 'YanetGarcia1337', 'Rapisher', 'Kizuro', 'Nataa', 'Huunntér', 'Hades147', 'Yolo Que Warrior']
Comments