{
"query": {
"pages": {
"7412236": {
"pageid": 7412236,
"ns": 0,
"title": "Steve Jobs",
"extract": "<p><b>Steven Paul</b> \"<b>Steve</b>\" <b>Jobs</b> (/\u02c8d\u0292\u0252bz/; February 24, 1955\u00a0\u2013 October 5, 2011) was an American entrepreneur, marketer, and inventor, who was the co-founder (along with Steve Wozniak and Ronald Wayne), chairman, and CEO of Apple Inc. Through Apple, he is widely recognized as a charismatic pioneer of the personal computer revolution and for his influential career in the computer and consumer electronics fields, transforming \"one industry after another, from computers and smartphones to music and movies\". Jobs also co-founded and served as chief executive of Pixar Animation Studios; he became a member of the board of directors of The Walt Disney Company in 2006, when Disney acquired Pixar. Jobs was among the first to see the commercial potential of Xerox PARC's mouse-driven graphical user interface, which led to the creation of the Apple Lisa and, one year later, the Macintosh. He also played a role in introducing the LaserWriter, one of the first widely available laser printers, to the market.</p>\n<p>After a power struggle with the board of directors in 1985, Jobs left Apple and founded NeXT, a computer platform development company specializing in the higher-education and business markets. In 1986, he acquired the computer graphics division of Lucasfilm, which was spun off as Pixar. He was credited in <i>Toy Story</i> (1995) as an executive producer. He served as CEO and majority shareholder until Disney's purchase of Pixar in 2006. In 1996, after Apple had failed to deliver its operating system, Copland, Gil Amelio turned to NeXT Computer, and the NeXTSTEP platform became the foundation for the Mac OS X. Jobs returned to Apple as an advisor, and took control of the company as an interim CEO. Jobs brought Apple from near bankruptcy to profitability by 1998.</p>\n<p>As the new CEO of the company, Jobs oversaw the development of the iMac, iTunes, iPod, iPhone, and iPad, and on the services side, the company's Apple Retail Stores, iTunes Store and the App Store. The success of these products and services provided several years of stable financial returns, and propelled Apple to become the world's most valuable publicly traded company in 2011. The reinvigoration of the company is regarded by many commentators as one of the greatest turnarounds in business history.</p>\n<p>In 2003, Jobs was diagnosed with a pancreas neuroendocrine tumor. Though it was initially treated, he reported a hormone imbalance, underwent a liver transplant in 2009, and appeared progressively thinner as his health declined. On medical leave for most of 2011, Jobs resigned in August that year, and was elected Chairman of the Board. He died of respiratory arrest related to his tumor on October 5, 2011.</p>\n<p>Jobs received a number of honors and public recognition for his influence in the technology and music industries. He has been referred to as \"legendary\", a \"futurist\" or simply \"visionary\", and has been described as the \"Father of the Digital Revolution\", a \"master of innovation\", \"the master evangelist of the digital age\" and a \"design perfectionist\".</p>\n<p></p>"
}
}
}
}
因此,我使用以下代码
以JSON格式获取Wikipedia API提供的内容fetchedPage = urllib2.urlopen('https://en.wikipedia.org/w/api.php?action=query&prop=extracts&exintro&titles=Steve%20Jobs&format=json')
Json = json.load(fetchedPage)
content = Json['query']['pages']['7412236']['extract']
print content
它适用于单个文章,因为我可以手动输入文章&#34; pageid&#34;。但是在更一般的基础上,我必须跳过&#34; pageid&#34;元素,以便我可以直接获取任何文章的内容。
简而言之,我希望实现这样的目标,
content = Json['query']['pages'][//I dont care what's in this element]['extract']
我该如何处理?
答案 0 :(得分:2)
您可以在没有密钥的情况下通过dict.itervalues().next()
访问字典的第一个元素。 itervalues()
返回字典中的可迭代值,next()
返回此可迭代的第一个元素。
fetchedPage = urllib2.urlopen('https://en.wikipedia.org/w/api.php?action=query&
prop=extracts&exintro&titles=Steve%20Jobs&format=json')
Json = json.load(fetchedPage)
content = Json['query']['pages'].itervalues().next()['extract']
print content
另一种方法是,如果您觉得它更明确,可以使用dict[dict.keys()[0]]
获取dict的第一个键。在您的示例中,您将拥有:
page_id = Json['query']['pages'].keys()[0]
content = Json['query']['pages'][page_id]['extract']
答案 1 :(得分:2)
这将为你的词典中的所有“页面”构建一个“摘录”列表:
extracts = [p["extract"] for p in json["query"]["pages"].values()]