我正在尝试从Bing API返回的JSON中提取所有DisplayUrl
值。我不确定我做错了什么。任何见解都表示赞赏!
search_response = urllib2.urlopen(request)
search_results = search_response.read()
results = json.loads(search_results)
for item in results.get(u'Web', []):
print item.get(u'DisplayUrl')
我的请求被配置为返回两个结果,所以我希望从DisplayUrl
键获得两次打印。
收到的results
:
{
u'd': {
u'results': [
{
u'Web': [
{
u'Description': u'Test.comprovidesacompletesoftwaresolutionforcreatingonlinetestsandmanagingenterpriseandspecialistcertificationprograms,
inupto22languages.',
u'Title': u'Test',
u'Url': u'http: //www.test.com/',
u'__metadata': {
u'type': u'WebResult',
u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/ExpandableSearchResultSet(guid'51264a7c-1d14-44a2-bbcc-afa43e7fac29')/Web?$skip=0&$top=1"
},
u'DisplayUrl': u'www.test.com',
u'ID': u'6987b446-c20a-4521-a998-f7cf62cff0aa'
},
{
u'Description': u"Test your Internet Connection with Speakeasy's reliable and accurate broadband speed test. What's your speed?",
u'Title': u'SpeakeasySpeedTest-MegaPath',
u'Url': u'http: //www.speakeasy.net/speedtest/',
u'__metadata': {
u'type': u'WebResult',
u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/ExpandableSearchResultSet(guid'51264a7c-1d14-44a2-bbcc-afa43e7fac29')/Web?$skip=1&$top=1"
},
u'DisplayUrl': u'www.speakeasy.net/speedtest',
u'ID': u'b2cab0a5-9866-481b-86aa-1621616ca9c9'
}
],
u'VideoTotal': u'',
u'RelatedSearch': [
],
u'Image': [
],
u'__metadata': {
u'type': u'ExpandableSearchResult',
u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Composite?Sources='web'&Query='test.com'&$skip=0&$top=1"
},
u'ImageOffset': u'',
u'AlterationOverrideQuery': u'',
u'ImageTotal': u'',
u'WebTotal': u'707000000',
u'SpellingSuggestionsTotal': u'',
u'WebOffset': u'0',
u'Video': [
],
u'News': [
],
u'AlteredQuery': u'',
u'SpellingSuggestions': [
],
u'VideoOffset': u'',
u'NewsTotal': u'',
u'ID': u'51264a7c-1d14-44a2-bbcc-afa43e7fac29',
u'NewsOffset': u''
}
]
}
}
答案 0 :(得分:1)
你有一个外包装在这里穿行;您的results
对象是一个包含一个键'd'
的字典,引用一个具有'results'
键的字典值,这是一个字典列表,那里我们最终找到了一个带有'Web'
键的字典:
for result in results['d']['results']:
for item in result.get('Web', []):
print item.get(u'DisplayUrl')
演示:
>>> for result in results['d']['results']:
... for item in result.get('Web', []):
... print item.get(u'DisplayUrl')
...
www.test.com
www.speakeasy.net/speedtest
答案 1 :(得分:-1)
它比看起来更嵌套。
以下是docs:
的json输出示例"Web":{
"Total":5100,
"Offset":0,
"Results":[
{
"Title":"Testign part 2 - Tiernan OTooles Programming Blog",
"Description":"If this works, it means nothing really, but i have managed to build a .TEXT blog posting app. could be handy if i move my main blog to .TEXT, which i am thinking about..",
"Url":"http:\/\/weblogs.asp.net\/tiernanotoole\/archive\/2004\/09\/24\/233830.aspx",
"DisplayUrl":"http:\/\/weblogs.asp.net\/tiernanotoole\/archive\/2004\/09\/24\/233830.aspx",
"DateTime":"2008-10-21T05:08:05Z"
}
请注意,DisplayUrl
位于列表中,并使用Results
密钥存储。所以,得到它:
for item in results.get('Web'. {}).get('Results', []):
print item.get('DisplayUrl', "Not Found")
希望这有帮助!