string - For Loop List Python for Variable URL -
so if wanted print contents of url page or cycle through many , change few segments of url how might so. given following:
if know format following player, minus few tweeks id , last portion: format below:
http://espn.go.com/mlb/player/_/id/31000/brad-brach
lets know each players id , name:
player_name = ['brad-brach','oliver-drake',...] player_id = ['31000','31615',...] in player_id: url = 'http://espn.go.com/mlb/player/_/id/'+player_id[i]+/'+player_name[i]
do ever given know these players in player_id , player_name.
how might iterate through player_id's , player_name's without getting
typeerror: list indices must integers, not str
i know url list , contents within player_id[0] string. missing here?
select item list index not string of list, player_name['31000']?!
player_name = ['brad-brach','oliver-drake',...] player_id = ['31000','31615',...] in xrange(len(player_name)): url = 'http://espn.go.com/mlb/player/_/id/{}/{}'.format(player_id[i], player_name[i])
and more elegant solution use zip, @pynchia ;)
player_name = ['brad-brach','oliver-drake',...] player_id = ['31000','31615',...] url_pattern = 'http://espn.go.com/mlb/player/_/id/{}/{}' p_name, p_id in zip(player_name, player_id): url = url_pattern.format(p_id, p_name)
Comments
Post a Comment