2

So if I wanted to print the contents of a url page or just cycle through many and only change a few segments of the URL how might I do so. Given the following:

If I know the format will usually be the following for any player, minus a few tweeks to the ID and last portion: Format below:

http://espn.go.com/mlb/player/_/id/31000/brad-brach

Lets say I know each players ID and name:

PLAYER_NAME = ['brad-brach','oliver-drake',...]
PLAYER_ID = ['31000','31615',...]
for i in PLAYER_ID:
url = 'http://espn.go.com/mlb/player/_/id/'+PLAYER_ID[i]+/'+PLAYER_NAME[i]

Do what ever given that we know all these players in the PLAYER_ID and PLAYER_NAME.

How might I iterate through all the PLAYER_ID's and PLAYER_NAME's without getting a

TypeError: list indices must be integers, not str

I know that url is a list and the contents within it PLAYER_ID[0] would be a string. What am I missing here?

4
  • for player in PLAYER_NAME: is what you're looking for Commented Sep 29, 2015 at 21:42
  • Now place player in your url string. Not PLAYER_ID that is a list Commented Sep 29, 2015 at 21:44
  • You're adding lists (player_id and player_name) to a string (url) Commented Sep 29, 2015 at 21:45
  • PLAYER_NAMES and PLAYER_IDS would be better names for collections. Commented Sep 29, 2015 at 21:58

2 Answers 2

1

Select an item from list by index not the string of another list, PLAYER_NAME['31000']?!

PLAYER_NAME = ['brad-brach','oliver-drake',...]
PLAYER_ID = ['31000','31615',...]
for i in xrange(len(PLAYER_NAME)):
    url = 'http://espn.go.com/mlb/player/_/id/{}/{}'.format(PLAYER_ID[i], PLAYER_NAME[i])

And for an even more elegant solution use zip, thanks to @Pynchia ;)

PLAYER_NAME = ['brad-brach','oliver-drake',...]
PLAYER_ID = ['31000','31615',...]
URL_PATTERN = 'http://espn.go.com/mlb/player/_/id/{}/{}'
for p_name, p_id in zip(PLAYER_NAME, PLAYER_ID):
    url = URL_PATTERN.format(p_id, p_name)
Sign up to request clarification or add additional context in comments.

5 Comments

I believe that this is closest to what OP wants...kind of hard to tell from their question, though.
So I was kind of close
get the glory, I was just pulling your leg. :)
fix it and add the version with zip to make your answer more complete
"OP" = "Original Post[er]"
0

for something in container will give you each item in the container; this is a very clean way of iterating:

>>> for i in PLAYER_ID:
...     print i
31000
31615

When you use PLAYER_ID[i], what you really want is the index. You can get that by enumerating every element in the list:

>>> for i, element in enumerate(PLAYER_ID):
...     print i, element
0 31000
1 31615

However, you don't really need the index, as you already have a clean way of getting the player's ID.

As I mentioned in a comment, better names would be PLAYER_IDS and PLAYER_NAMES, so that:

>>> for PLAYER_NAME, PLAYER_ID in zip(PLAYER_NAMES, PLAYER_IDS):
...     print PLAYER_NAME, PLAYER_ID
brad-brach 31000
oliver-drake 31615

3 Comments

So in order to print the stats of these players, I would need to for-loop to execute both players stats. I would need to read the customized url. So it would read on the first iteration: url: espn.go.com/mlb/player/_/id/31000/brad-brach then on the second iteration url would be espn.go.com/mlb/player/_/id/31615/oliver-drake
@RossJohnson Hey, those urls actually work! Nice job.
Ah ha! Got it! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.