在下面對 Solr 有一個簡單的請求
response = requests.get("http://localhost:8080/solr/select?fl=id,title,description,keywords,author&q=domain:mydomain.com&rows=10&indent=on&wt=json&omitHeader=true")
result = response.json()
print type(result)
print result
print response.text
上面的代碼給我下面的輸出
型別
<type 'dict'>
字典內容
{u'response': {u'start': 0, u'numFound': 1, u'docs': [{u'keywords': u'my keywords listed here', u'id': u'project.websiteindex.abe919664893e46eef21aacb1360948ae836a756faa28e2063a4a92ef4273630', u'description': u'my site description isted here.', u'title': u'my site title'}]}}
并低于可讀性更好的版本
{
"response":{"numFound":1,"start":0,"docs":[
{
"description":"my site description isted here.",
"title":"my site title",
"keywords":"my keywords listed here",
"id":"project.websiteindex.abe919664893e46eef21aacb1360948ae836a756faa28e2063a4a92ef4273630"}]
}}
執行:
print(result['response']['numFound'])
會給我非常有用的輸出
1
非常好,但顯然我想得到類似的東西
print(result['response']['title'])
my site title
所以我的問題是,我如何將標題、描述和關鍵字的值作為鍵我如何使用這種格式選擇它們,或者如何以更好的 dict 格式獲得我想要的值?
ps 在這里使用 python 2.7 和 solr 3.6 的舊設定
uj5u.com熱心網友回復:
由于您要尋址的值是陣列元素的欄位,因此您必須首先尋址該元素。
嘗試類似:
result['response']['docs'][0]['title']
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/482254.html
