如何以JSON格式获取多个图层

时间:2015-06-14 14:32:00

标签: python json

我使用的是pyhton3.4.1。

我正在使用谷歌自定义搜索。

我想获得链接,但它是diplay TypeError:字符串索引必须是整数。

以下是我的代码和JSON格式。

from urllib.request import urlopen
import json
u = urlopen('https://www.googleapis.com/customsearch/v1?key=AIzaSyC3jpmwO3Ieifw1VnrVoL3mS3KSE_GMRvo&cx=010407088344546736418:onjj7gscy2g&q=lol&num=10')
resp = json.loads(u.read().decode('utf-8'))
for link in resp:
  for k in link['item']:
    print(k['link'])

和JSON fomat如下所示。

 "items": [
{
"kind": "customsearch#result",
"title": "League of Legends",
"htmlTitle": "<b>League of Legends</b>",
"link": "http://leagueoflegends.com/",
"displayLink": "leagueoflegends.com",
"snippet": "Official site. Features, media, screenshots, FAQs, and forums.",
"htmlSnippet": "Official site. Features, media, screenshots, FAQs, and   forums.",
"cacheId": "GCRD1wy5e3QJ",
"formattedUrl": "leagueoflegends.com/",
"htmlFormattedUrl": "<b>leagueoflegends</b>.com/",
"pagemap": {
"cse_image": [
 {
  "src": "http://na.leagueoflegends.com/sites/default/files/styles/wide_small/public/upload/pool_party_201_splash_1920.jpg?itok=QGxFrikL"
 }
],
"cse_thumbnail": [
 {
  "width": "256",
  "height": "144",
  "src": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSvyCGlnn9a7N13rjwbPvSNemH-mbqzC6otkcJgeOK-6c1dkcMP6XIumTXG"
 }
],

1 个答案:

答案 0 :(得分:0)

将最后3行更改为:

for item in resp['items']:
    print(item['link'])