0

I'm scraping some pages to gather data on athletes. I start off like so:

req = requests.get(url)
soup = BeautifulSoup(req.text, "html.parser")

and I go on to get things like the athlete's name, team, height, weight, etc.

Once I've finished gathering the data, I want to create an instance of my athlete class. Obviously, when testing, I can just do something like this:

david_ortiz = athlete.Athlete(name, team, height, weight)

My problem is that I don't know how to name these new instances (outside of when I'm making instances like david_ortiz for testing). How do I name these instances without hardcoding the athletes name in advance? It's also possible that there could be multiple athletes with the same name so maybe this wouldn't be the best solution? If that's the case, what do people do in this situation?

UrsinusTheStrong
  • 1,239
  • 1
  • 16
  • 33
CGul
  • 147
  • 1
  • 3
  • 11
  • 1
    Make a dictionary with the names as the keys, instead of separate variables. If there can be more than one player with the same name, use something else as the key instead (e.g., some kind of player ID, or a combination of the name with the player's team, birthdate, etc.). – BrenBarn Jun 13 '16 at 17:47

1 Answers1

0

I think you can use dict to store name you get from web data

allAthlete = { name : athlete.Athlete(name, team, height, weight) }

or

allAthlete = {}
allAthlete[name] = athlete.Athlete(name, team, height, weight)

when you want to access the data in dict:

allAthlete.get(name,None)

to call method of athlete:

allAthlete.get(name,None).height
galaxyan
  • 5,944
  • 2
  • 19
  • 43
  • Thanks! @galaxyan Now if I want to call a particular instance of the athlete class, how would I do that? Again, let's say I scrape David Ortiz's page and I store the instance in the all_athlete dictionary. When I try `print all_athletes` it looks like the name of the instance is the string 'David Ortiz.' Yet, if I run `print 'David Ortiz'.height` I get an error that says str object has no attribute 'height.' So how can I call, and reference, the newly created instance? – CGul Jun 13 '16 at 23:53
  • @CGul you can access the dict. answer edited – galaxyan Jun 14 '16 at 00:30