Information comes from the server in this form:

{ 'date': 1539251604, 'from_id': 511076166, 'id': 13135, 'out': 1, 'peer_id': 2000000007, 'text': '', 'conversation_message_id': 6231, 'fwd_messages': [], 'important': False, 'random_id': 521048654, 'attachments': [ { 'type': 'photo', 'photo': { 'id': 456239156, 'album_id': -3, 'owner_id': 511076166, 'sizes': [ { 'type': 's', 'url': 'https://pp.userapi.com/c851016/v851016735/1fed3/WokhTUD_2Mc.jpg', 'width': 1, 'height': 1 }, { 'type': 'm', 'url': 'https://pp.userapi.com/c851016/v851016735/1fed4/5Gqb9dlJIOE.jpg', 'width': 1, 'height': 1 }, { 'type': 'x', 'url': 'https://pp.userapi.com/c851016/v851016735/1fed5/nxejisifcP8.jpg', 'width': 1, 'height': 1 }, { 'type': 'o', 'url': 'https://pp.userapi.com/c851016/v851016735/1fed6/WKu01Esn_s0.jpg', 'width': 1, 'height': 1 }, { 'type': 'p', 'url': 'https://pp.userapi.com/c851016/v851016735/1fed7/Ts8l1jwE7ys.jpg', 'width': 1, 'height': 1 }, { 'type': 'q', 'url': 'https://pp.userapi.com/c851016/v851016735/1fed8/WlxWkF4gdl4.jpg', 'width': 1, 'height': 1 }, { 'type': 'r', 'url': 'https://pp.userapi.com/c851016/v851016735/1fed9/1PI5cwmKqr4.jpg', 'width': 1, 'height': 1 } ], 'text': '', 'date': 1539251598, 'access_key': 'af125b9950b452218a' } }, { 'type': 'photo', 'photo': { 'id': 456239157, 'album_id': -3, 'owner_id': 511076166, 'sizes': [ { 'type': 's', 'url': 'https://pp.userapi.com/c851016/v851016735/1feda/Kkx3EKlIoqs.jpg', 'width': 1, 'height': 1 }, { 'type': 'm', 'url': 'https://pp.userapi.com/c851016/v851016735/1fedb/JrzeNLaY0nQ.jpg', 'width': 1, 'height': 1 }, { 'type': 'x', 'url': 'https://pp.userapi.com/c851016/v851016735/1fedc/sn2sNSL-ajo.jpg', 'width': 1, 'height': 1 }, { 'type': 'o', 'url': 'https://pp.userapi.com/c851016/v851016735/1fedd/mt1Rj3RPDCo.jpg', 'width': 1, 'height': 1 }, { 'type': 'p', 'url': 'https://pp.userapi.com/c851016/v851016735/1fede/2cViz9gNm7c.jpg', 'width': 1, 'height': 1 }, { 'type': 'q', 'url': 'https://pp.userapi.com/c851016/v851016735/1fedf/n0VGo-Askwk.jpg', 'width': 1, 'height': 1 }, { 'type': 'r', 'url': 'https://pp.userapi.com/c851016/v851016735/1fee0/f6UwVoiPYls.jpg', 'width': 1, 'height': 1 } ], 'text': '', 'date': 1539251602, 'access_key': 'ea1302d738f5092d1c' } } ], 'is_hidden': False } 

I need to get from this entire text all the links that come after the 'type': 'r', 'url': (there are 2 of them here). The question is how to implement it?

  • The code lacks closing elements. - Enikeyschik
  • Corrected. Now everything seems to be there. - user311842
  • for x in словарь['attachments'] and inside for size in x['sizes'] plus minor additional checks - andreymal
  • Gives an error KeyError: 'sizes' - user311842
  • It was sealed up x['photo']['sizes'] . Although you have to understand all this yourself if you have read at least one python tutorial - andreymal

1 answer 1

This is without checks, and, probably, you will need to do them:

 rs = { 'date': 1539251604, 'from_id': 511076166, ... } for attachment in rs['attachments']: for img in attachment['photo']['sizes']: if img['type'] != 'r': continue print(img['url']) 

Console:

 https://pp.userapi.com/c851016/v851016735/1fed9/1PI5cwmKqr4.jpg https://pp.userapi.com/c851016/v851016735/1fee0/f6UwVoiPYls.jpg 
  • @ Enikeyschik, thanks for the amendment :) - gil9red
  • Thank you, it works. - user311842
  • one
    @ user311842, then mark the answer as accepted :) - gil9red