0

代码为该行生成错误“列表索引必须是 str”

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'] for res in r.json()['d']['results']]
    urlss = [res['Url'] for res in r.json()['d']['results']]
    return urlss
r = request("Jason Bourne")
for res in r['d']['results']:
    print res['Url']

我怎样才能避免这个错误?

错误:

Traceback (most recent call last):
  File "C:\Python27\Project\YQL.py", line 15, in <module>
    for res in r['d']['results']:
TypeError: list indices must be integers, not str

函数外的 print r 返回:

[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']

我如何从中提取网址?

4

1 回答 1

2

Your request function returns a list:

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

which you then bind to r:

    r = request("Jason Bourne")

If you want to avoid the error, you can't loop over r['d']['results'], since r is a list; you'll need to (at the very least) change the loop to 'for res in r:' and then dig into each element from there.

于 2013-08-13T02:06:40.373 回答