import requests
URL = "https://mykey:[email protected]/Bing/Search/Web?$format=json&Query=%(query)s"
API_KEY = 'mykey'
def request(query, **params):
query = ('%27'+query+ '%27')
r = requests.get(URL % {'query': query}, auth=('', API_KEY))
print r.content
#return r.json()['d']['results']
r = request("JasonBourne")
print r[0]['Url']
它產生我在上面提到的TypeError:'NoneType'對象沒有屬性'__getitem__'。 Bing搜索
print r[0]['Url']
r.content值誤差
{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query=\u0027JasonBourne\u0027&$skip=0&$top=1","type":"WebResult"},"ID":"caac4e4d-b701-497a-ae61-f2b2993685d5","Title":"Jason Bourne - Wikipedia, the free encyclopedia","Description":"Jason Charles Bourne is a fictional character and the protagonist of a series of novels by Robert Ludlum and subsequent film adaptations. He first appeared in the ...","DisplayUrl":"en.wikipedia.org/wiki/Jason_Bourne","Url":"http://en.wikipedia.org/wiki/Jason_Bourne"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query=\u0027JasonBourne\u0027&$skip=1&$top=1","type":"WebResult"},"ID":"32688769-2490-470e-9903-8ea6b836ff97","Title":"JasonBourne.com","Description":"Welcome to Loscocco Media ... Created by author Robert Ludlum and brought to the silver screen with the face of Matt Damon, Jason Bourne is an enigmatic killer in ...","DisplayUrl":"jasonbourne.com","Url":"http://jasonbourne.com/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query=\u0027JasonBourne\u0027&$skip=2&$top=1","type":"WebResult"},"ID":"5db32b8f-2940-4542-a2e7-379e347201c3","Title":"Jason Bourne | The Bourne Identity | The Bourne Ultimatum | The ...","Description":"Jason Bourne is a CIA assassin & the hero of a trilogy of movies based on the books of Robert Ludlam; these are The Bourne Identity, Bourne Ultimatum, & Bourne Supremacy.","DisplayUrl":"jason-bourne.com","Url":"http://jason-bourne.com/"},{"__metadata":}
這並不是全部價值,因爲它太大了。
assert r [0]不是無 –
爲什麼你評論'return r.json()['d'] ['results']'?那至少會返回* * *(前提是你獲得了結果)。 –