2016-04-05 58 views
0

我正在使用維基百科API - wikitools軟件包從維基百科中提取一些數據。我得到如下所示格式的輸出,現在我想提取特定用戶對幾個頁面進行修訂的時間戳和註釋。比方說,我只是想通過TechBot提出的意見,那麼我想,我可以這樣做:使用Wikipedia API和Python 2.7從列表中提取特定用戶評論

for revision in res["query"]["pages"]["7940378"]["revisions"]: 
    if revision["user"] = "Techbot": 
     do.something() 

但問題是[「7940378」],因爲這是一個獨特的頁ID,併爲每個頁面更改我不知道如何獲得pageid。有沒有另外一種方法呢?

[{ 
"query": { 
    "pages": { 
    "7940378": { 
     "ns": 0, 
     "pageid": 7940378, 
     "revisions": [ 
     { 
      "comment": "robot Modifying: [[az:T\u00fcrk Tarixi]]", 
      "timestamp": "2009-01-03T19:47:11Z", 
      "user": "TechBot" 
     }, 
     { 
      "comment": "", 
      "timestamp": "2009-02-14T02:07:49Z", 
      "anon": "", 
      "user": "88.231.237.130" 
     }, 
     { 
      "comment": "fixing recent deletion by merging it with the next paragraph", 
      "timestamp": "2009-04-03T14:49:27Z", 
      "user": "Soap" 
     }, 
     { 
      "comment": "robot Modifying: [[az:T\u00fcrk tarixi]]", 
      "timestamp": "2009-04-09T14:35:19Z", 
      "user": "RibotBOT" 
     }, 
     { 
      "comment": "Repairing link to disambiguation page - [[Wikipedia:Disambiguation pages with links|You can help!]]", 
      "timestamp": "2009-06-12T23:55:55Z", 
      "user": "J04n" 
     } 
     ], 
     "title": "History of the Turkic peoples" 
    } 
    } 
}, 
"continue": { 
    "rvcontinue": "20090807172715|306635892", 
    "continue": "||" 
}, 
"warnings": { 
    "main": { 
    "*": "Unrecognized parameter: 'user'" 
    } 
} 
}] 

回答

1

而不是使用一個for循環。你可以分成兩個循環,外循環獲取頁面,內循環可以到達修訂版本。

for pageid, pagedetails in res["query"]["pages"].iteritems(): 
    for revision in pagedetails["revisions"]: 
     if revision["user"] == "TechBot": 
      do.something() 
+0

這是行不通的,因爲我無法從'頁面'訪問'revisions'。 '在頁面[「修訂」]修訂:TypeError:字符串索引必須是整數' – Knokkelgeddon

+0

我的不好。我沒有注意到該頁面將是必需的關鍵。我編輯了正確答案 – Anbarasan