TypeError: 'instancemethod' object has no attribute '__getitem__'

Asked by Rajesh kannan on 2013-01-25

Hi every one..

I'm trying to run the following python code..

    import urllib3,urllib,requests,xml,json,sys

site="http://en.wikipedia.org/w/api.php"
sites_res=[]
titles_res=[]

def openSearch(kw):
 params={}
 params['action']="opensearch"
 params['search']=str(kw)
 params['format']="json"
 req=site+"?"+urllib.urlencode(params)
 print req
 res=requests.get(req)
 return res

def getByTitles(kw):
 params={}
 params['titles']=str(kw)
 params['prop']="info"
 params['inprop']="url"
 params['action']="query"
 params['format']="json"
 req=site+"?"+urllib3.urlencode(params)
 res=requests.get(req)
 res=res.json
 for pg in res['query']['pages']:
   titles_res.append(str(res['query']['pages'][pg]['title']))
   sites_res.append(str(res['query']['pages'][pg]['fullurl']))

def getListTitles(res):
 for pg in res['query']['search']:
   getByTitles(str(pg['title']))
 #maxlen=max([len(x) for x in titles_res])
 #for x in range(len(titles_res)):
   #print titles_res[x].ljust(maxlen)+"-".center(9)+sites_res[x]

def query(kw,limit=10,type="g"):
 global sites_res
 sites_res=[]
 params={}
 params['action']="query"
 #print type
 if (type=="l"):
  params['list']="search"
  params['srsearch']=str(kw)
  params['srlimit']=int(limit)
 else:
  params['gsrsearch']=str(kw)
  params['generator']="search"
  params['gsrlimit']=int(limit)
  params['prop']="info"
  params['inprop']="url"
 params['format']="json"
 req=site+"?"+urllib.urlencode(params)
 #print req
 res=requests.get(req)
 if (type=="l"):
  print res.json
  getListTitles(res.json)
  #parser(res.json,type="l")
 else:
  parser(res.json)
 return res,sites_res

def parser(res,type="g"):
 global sites_res
 results=res
 title=[]
 url=[]
 wordcount=[]
 size=[]
 if (type=="g"):
  for pg in results['query']['pages']:
   title.append(str(results['query']['pages'][pg]['title']))
   url.append(str(results['query']['pages'][pg]['fullurl']))
   size.append(str(results['query']['pages'][pg]['length']))
  sites_res=url
  maxlen=max([len(x) for x in title])
  #print maxlen
  #maxlenl=max([len(str(x)) for x in size])
  for x in range(len(title)):
   print title[x].ljust(maxlen)+"-".center(9)+url[x]
 else:
  for pg in results['query']['search']:
   title.append(str(pg['title']))
   wordcount.append(str(pg['wordcount']))
   size.append(str(pg['size']))
  maxlen=max([len(x) for x in title])
  maxlenwc=max([len(str(x)) for x in wordcount])
  for x in range(len(title)):
   print title[x].ljust(maxlen)+"-".center(5)+wordcount[x].ljust(maxlenwc+2)+"-".center(5)+size[x]

a,b=query("Dennis",limit=6,type="l")
print a,b

while running this program, I got the following error.

    File "/home/sysop/wiki.py", line 33, in getListTitles
    for pg in res['query']['search']:
TypeError: 'instancemethod' object has no attribute '__getitem__'

I dunno what to do.. any one who knows the solution.. please help me...

Thanks & Regards
Rajesh kannan.R

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu python2.7 Edit question
Assignee:
No assignee Edit question
Solved by:
Manfred Hampl
Solved:
2013-01-28
Last query:
2013-01-28
Last reply:
2013-01-25
Best Manfred Hampl (m-hampl) said : #1

Googling for the error text provides some hits, among others
https://github.com/kennethreitz/requests/issues/1005#issuecomment-11465153
Maybe using res.json() instead of res.json as written there helps.

Thank u Mafred.. Thank u so much for your valuable time..

Its worked and solved my problem

Thanks Manfred Hampl, that solved my question.