Nigel Silva
Nigel Silva

Reputation: 1

Python - TypeError: string indices must be integers - Instagram Bot

I am working on an instagram bot that automatically likes pictures and follows people. I am having a really hard time getting the JSON to work. Here is the Python:

        # Make a list of users who are currently being followed, or have been followed before
    already_followed = []
    for tile in tiles['present']:
        already_followed.append(tile['user_id'])
    for tile in tiles['past']:
        already_followed.append(tile['user_id'])

This is my JSON file:

    {
  "present": {
    "user": {}
  },
  "past": {
    "user": {}
  }
}

And this is the error:

  File "Instagram-bot.py", line 95, in <module>
    already_followed.append(tile['user_id'])
TypeError: string indices must be integers

If you guys need anything else, let me know!

Upvotes: 0

Views: 157

Answers (1)

Vivek Sable
Vivek Sable

Reputation: 10223

yes, tile is string type i.e. key from the tiles['present'] dictionary.

try to print type of variable tile in code. e.g.

>>> a = 123
>>> type(a)
<type 'int'>
>>> a = "123"
>>> type(a)
<type 'str'>
>>> 

sample code:

tiles = {
  "present": {
    "user": {"user_id": 123}
  },
  "past": {
    "user": {"user_id": 456}
  }
}

already_followed = []
for tile in tiles['present']:
    already_followed.append(tiles['present'][tile]['user_id'])

for tile in tiles['past']:
        already_followed.append(tiles['past'][tile]['user_id'])


print "already_followed:-", already_followed

output:

vivek@vivek:~/Desktop/stackoverflow$ python 5.py 
already_followed:- [123, 456]

Upvotes: 1

Related Questions