There is a result with json (VK API):

{ "response": { "count": 1675, "items": [{ "id": 1835, "from_id": -156603484, "owner_id": -156603484, "date": 1517756029, "marked_as_ads": 0, "post_type": "post", "text": "#artist_baekgup #cute #dashabetes #equestriagirls #fistbump #heart #hoofbump #humanponidox #rainbowdash #safe #selfponidox #squarecrossover", "can_edit": 1, "created_by": 213468131, "can_delete": 1, "can_pin": 1, "attachments": [{ "type": "photo", "photo": { "id": 456240790, "album_id": -7, "owner_id": -156603484, "user_id": 213468131, "photo_75": "https://pp.userap...b09/4PbsucV0-4g.jpg", "photo_130": "https://pp.userap...b0a/vHVTRcXyvyY.jpg", "photo_604": "https://pp.userap...b0b/6cccdrLMecQ.jpg", "width": 551, "height": 500, "text": "", "date": 1517756029, "post_id": 1835, "access_key": "1be80adf605e9c36d8" } }], "post_source": { "type": "api" }, "comments": { "count": 0, "groups_can_post": true, "can_post": 1 }, "likes": { "count": 1, "user_likes": 0, "can_like": 1, "can_publish": 1 }, "reposts": { "count": 0, "user_reposted": 0 }, "views": { "count": 2 } }], "profiles": [{ "id": 213468131, "first_name": "Тимур", "last_name": "Ивченко", "sex": 2, "screen_name": "jengas", "photo_50": "https://pp.userap...328/iymw6YlJsRQ.jpg", "photo_100": "https://pp.userap...327/J9eLMYmV_LI.jpg", "online": 1 }], "groups": [{ "id": 156603484, "name": "Equestria in the Space", "screen_name": "equestriaspace", "is_closed": 0, "type": "group", "is_admin": 1, "admin_level": 3, "is_member": 1, "photo_50": "https://pp.userap...089/u0_mBSE4E34.jpg", "photo_100": "https://pp.userap...088/O6vENP0IW_w.jpg", "photo_200": "https://pp.userap...086/rwntMz6YwWM.jpg" }] } } 

(A screenshot is more visible http://prntscr.com/i9z1i0 ) And I need to get the "id": 1835, so that the console displays ( 1835 ) But the code:

 method_url = 'https://api.vk.com/method/wall.get?' data = dict(access_token=access_token, gid=group_id) response = requests.post(method_url, data) results = json.loads(response.text) postidd = results['response']['items'][0]['id'] print(postidd) 

But the code does not work, writes an error KeyError: 'response'

  • print(postidlist) will tell you the answer to the question - andreymal
  • What does print(results) show? - jfs
  • You can be perverted to run the loop in the results ['response'], and already in it to go further - Shihkauskas

1 answer 1

 >>> import json >>> m = '{"response":{"count":1675,"items":[{"id":1835,"from_id":-156603484,"owner_id":-156603484,"date":1517756029,"marked_as_ads":0,"post_type":"post","text":"#artist_baekgup #cute #dashabetes #equestriagirls #fistbump #heart #hoofbump #humanponidox #rainbowdash #safe #selfponidox #squarecrossover","can_edit":1,"created_by":213468131,"can_delete":1,"can_pin":1,"attachments":[{"type":"photo","photo":{"id":456240790,"album_id":-7,"owner_id":-156603484,"user_id":213468131,"photo_75":"https://pp.userap...b09/4PbsucV0-4g.jpg","photo_130":"https://pp.userap...b0a/vHVTRcXyvyY.jpg","photo_604":"https://pp.userap...b0b/6cccdrLMecQ.jpg","width":551,"height":500,"text":"","date":1517756029,"post_id":1835,"access_key":"1be80adf605e9c36d8"}}],"post_source":{"type":"api"},"comments":{"count":0,"groups_can_post":true,"can_post":1},"likes":{"count":1,"user_likes":0,"can_like":1,"can_publish":1},"reposts":{"count":0,"user_reposted":0},"views":{"count":2}}],"profiles":[{"id":213468131,"first_name":"Тимур","last_name":"Ивченко","sex":2,"screen_name":"jengas","photo_50":"https://pp.userap...328/iymw6YlJsRQ.jpg","photo_100":"https://pp.userap...327/J9eLMYmV_LI.jpg","online":1}],"groups":[{"id":156603484,"name":"Equestria in the Space","screen_name":"equestriaspace","is_closed":0,"type":"group","is_admin":1,"admin_level":3,"is_member":1,"photo_50":"https://pp.userap...089/u0_mBSE4E34.jpg","photo_100":"https://pp.userap...088/O6vENP0IW_w.jpg","photo_200":"https://pp.userap...086/rwntMz6YwWM.jpg"}]}}' >>> l = json.loads(m) >>> lst = [] >>> lst = l["response"]["items"] >>> lst2 = lst[0] >>> lst2['id'] 

Console:

 1835 

Your code does not work, because it does not take into account that a list is nested in the "item" , and access to it must be done as an element of the list. lst is a list, and lst2 is a dictionary.