python - Program gives error "List indices must not be string" -


the code generates error 'list indices must str" line

for res in r['d']['results']:  import requests url = "https://mykey:mykey@api.datamarket.azure.com/bing/search/web?$format=json&query=%(query)s" api_key = 'my_key' def request(query, **params):     query = ('%27'+query+ '%27')     r = requests.get(url % {'query': query}, auth=('', api_key))     print [res['url'] res in r.json()['d']['results']]     urlss = [res['url'] res in r.json()['d']['results']]     return urlss r = request("jason bourne") res in r['d']['results']:     print res['url'] 

how can avoid error?

the error:

traceback (most recent call last):   file "c:\python27\project\yql.py", line 15, in <module>     res in r['d']['results']: typeerror: list indices must integers, not str 

print r outside func returns this:

[u'http://en.wikipedia.org/wiki/jason_bourne', u'http://en.wikipedia.org/wiki/bourne_(film_series)', u'http://jason-bourne.com/', u'http://praag.org/?p=8971', u'http://www.thebournelegacy.com/', u'http://www.amazon.com/bourne-collection-identity-supremacy-ultimatum/dp/b000w07ekw', u'http://www.imdb.com/character/ch0002110/', u'http://www.imdb.com/title/tt0258463/', u'http://jasonbourne.com/', u'http://www.newkerala.com/news/story/53506/edward-snowden-viewed-as-some-kind-of-jason-bourne-by-youth-john-mccain.html', u'http://www.barnesandnoble.com/s/jason-bourne-series-in-order', u'http://bourne.wikia.com/wiki/book:_jason_bourne', u'http://en.wikipedia.org/wiki/the_bourne_ultimatum_(film)', u'http://www.firstshowing.net/2008/more-sequels-fourth-jason-bourne-film-on-track-for-2010/', u'https://www.facebook.com/jasonbourne', u'http://www.ebay.com/sch/i.html?_nkw=jason+bourne', u'http://www.linkedin.com/pub/dir/jason/bourne', u'http://www.amazon.com/s?ie=utf8&page=1&rh=i%3aaps%2ck%3ajason%20bourne', u'https://myspace.com/jasonbournemusic', u'http://www.goodreads.com/series/40633-jason-bourne', u'http://www.youtube.com/user/jasonbourneabc123', u'http://www.jason-bourne.com/about.html', u'http://www.ebay.com/sch/i.html?_nkw=jason+bourne+movies', u'http://it.wikipedia.org/wiki/jason_bourne', u'http://www.youtube.com/watch?v=uj4dfcm4ml0', u'https://www.facebook.com/pages/jason-bourne/112488198763958', u'http://jasonbourne.org/', u'http://variety.com/t/jason-bourne/', u'http://www.myspace.com/bourneconspiracygame#!', u'http://www.linkedin.com/pub/jason-bourne/29/496/9aa', u'http://www.jason-bourne-books.com/', u'http://www.rottentomatoes.com/quiz/jason-bourne-1046088/', u'http://twitter.com/notjasonbourn', u'http://www.jason-bourne-books.com/books.php', u'http://jasonbournefilmseries.wikia.com/wiki/jason_bourne', u'http://www.ign.com/characters/jason-bourne', u'http://www.barnesandnoble.com/w/dvd-jason-bourne-collection/14560774?ean=25195017794', u'http://moviesblog.mtv.com/tag/jason-bourne/', u'http://www.fanpop.com/clubs/jason-bourne', u'http://photobucket.com/images/jason%20bourne/#!', u'http://fr.wikipedia.org/wiki/jason_bourne', u'http://www.theguardian.com/film/movie/147726/bourne-legacy', u'http://twitter.com/subsissyjason', u'http://www.shopwiki.com/l/jason-bourne-movies', u'http://www.huffingtonpost.com/2012/12/05/matt-damon-bourne-legacy_n_2244670.html', u'http://news.moviefone.com/2013/06/10/burt-reynolds-jason-bourne/', u'http://deadliestfiction.wikia.com/wiki/jason_bourne', u'http://howtofightlikejasonbourne.com/', u'http://surbrook.devermore.com/adaptationsmovie/asstd_movie/jason_bourne.html', u'http://www.who2.com/bio/jason-bourne'] 

how extract urls out of this?

your request function returns list:

    urlss = [res['url'] res in r.json()['d']['results']]     return urlss 

which bind r:

    r = request("jason bourne") 

if want avoid error, can't loop on r['d']['results'], since r list; you'll need (at least) change loop 'for res in r:' , dig each element there.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -