Andi
Andi

Reputation: 143

How to fix TypeError: string indices must be integers?

When run the code, it shows this error:

File "E:\python343\crawler.py", line 36, in <module>
    if(x2['type']=='status'):
TypeError: string indices must be integers

Here is the code:

#PRINTING YOUR FRIEND ALL "favorite_teams"
fs = g.get_object("me/friends")
for f in fs['data']:
    #print f
    #print f['name'],f['id']
    print (f['name'])
    for x in g.get_object(f['id'])['favorite_teams']:
        print (x['name'])


#SEACHING TOP 10 PAGE ON INPUTTING PAGE NAME AND OUTPUT AS PAGE ID 
x= g.request('search', {'q' : 'TaylorSwift', 'type' : 'page', 'limit' : 1000})['data'][0]['id']

non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)

#GET ALL STATUS POST ON PARTICULAR PAGE(X=PAGE ID)
for x1 in g.get_connections(x, 'feed')['data']:
    print (str(x1).translate(non_bmp_map))
    for x2 in x1:
        print (str(x2).translate(non_bmp_map))
        if(x2['type']=='status'):
            x2['message']

So how to do with the x2['type'] to fix the error??

Hope to get your help, thank you.

Upvotes: 0

Views: 1886

Answers (1)

rebeling
rebeling

Reputation: 728

x2 is a string and not a response object. Get around it and examine x2 in the else case:

if isinstance(x2, dict):
    if(x2['type']=='status'):
        x2['message']
else:
    # examine the data
    print 'Unexpected data:', type(x2), x2

Upvotes: 1

Related Questions