如何获取JSON格式的多层

杰伊

我正在使用pyhton3.4.1。

我正在使用Google自定义搜索。

我想获取链接,但显示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"
 }
],
轮渡

将最后3行更改为:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章