2012-07-24 3 views
1

Instagram API에서 가져온 JSON 객체가 다음과 같습니다 (제공된 count 매개 변수에 따라) n 개의 게시물을 가질 수 있습니다. 각 게시물 처리를 위해 JSON 객체를 개별적으로 반복합니다.

{ 
    "pagination": { 
    "next_url": "https:\/\/api.instagram.com\/v1\/users\/3\/media\/recent?access_token=184046392.f59def8.c5726b469ad2462f85c7cea5f72083c0&max_id=205140190233104928_3", 
    "next_max_id": "205140190233104928_3" 
    }, 
    "meta": { 
    "code": 200 
    }, 
    "data": [{ 
    "attribution": null, 
    "tags": [], 
    "type": "image", 
    "location": { 
     "latitude": 37.798594362, 
     "name": "Presidio Bowling Center", 
     "longitude": -122.459878922, 
     "id": 27052 
    }, 
    "comments": { 
     "count": 132, 
     "data": [{ 
     "created_time": "1342734265", 
     "text": "Distinguishing!", 
     "from": { 
      "username": "naiicamilos", 
      "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_53690312_75sq_1336573463.jpg", 
      "id": "53690312", 
      "full_name": "Naii Camilos" 
     }, 
     "id": "239194812924826175" 
     }, { 
     "created_time": "1342737428", 
     "text": "@kevin in Spanish Presidio means Jail", 
     "from": { 
      "username": "jm0426", 
      "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_25881992_75sq_1342156673.jpg", 
      "id": "25881992", 
      "full_name": "Juan Mayen" 
     }, 
     "id": "239221343768285211" 
     }, { 
     "created_time": "1342768120", 
     "text": "Good imagination", 
     "from": { 
      "username": "kidloca", 
      "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_193133903_75sq_1342032241.jpg", 
      "id": "193133903", 
      "full_name": "Khaleda Noon" 
     }, 
     "id": "239478811731694145" 
     }, { 
     "created_time": "1342775967", 
     "text": "Cwl!", 
     "from": { 
      "username": "awesomeath", 
      "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_179252164_75sq_1339745821.jpg", 
      "id": "179252164", 
      "full_name": "awesomeath" 
     }, 
     "id": "239544638740894674" 
     }, { 
     "created_time": "1342796153", 
     "text": "\u597d\u7f8e\u263a", 
     "from": { 
      "username": "hidelau", 
      "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_47295330_75sq_1342763977.jpg", 
      "id": "47295330", 
      "full_name": "Hide Lau" 
     }, 
     "id": "239713963951001995" 
     }, { 
     "created_time": "1343018007", 
     "text": "#mindfreak", 
     "from": { 
      "username": "info2021", 
      "profile_picture": "http:\/\/images.instagram.com\/profiles\/anonymousUser.jpg", 
      "id": "27664191", 
      "full_name": "info2021" 
     }, 
     "id": "241575017119224582" 
     }, { 
     "created_time": "1343068374", 
     "text": "@kevin please share and promote my last pic. This will be the new hype as instagram", 
     "from": { 
      "username": "thansy_mansy", 
      "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_189019343_75sq_1342951587.jpg", 
      "id": "189019343", 
      "full_name": "thansy_mansy" 
     }, 
     "id": "241997523093295303" 
     }, { 
     "created_time": "1343068382", 
     "text": "@kevin :P", 
     "from": { 
      "username": "thansy_mansy", 
      "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_189019343_75sq_1342951587.jpg", 
      "id": "189019343", 
      "full_name": "thansy_mansy" 
     }, 
     "id": "241997589589790922" 
     }] 
    }, 
    "filter": "Rise", 
    "created_time": "1342676212", 
    "link": "http:\/\/instagr.am\/p\/NQD4KAABKF\/", 
    "likes": { 
     "count": 4810, 
     "data": [{ 
     "username": "caitlyn_hammonds", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/anonymousUser.jpg", 
     "id": "198322184", 
     "full_name": "caitlyn_hammonds" 
     }, { 
     "username": "sophiafrancis", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_43092892_75sq_1340548333.jpg", 
     "id": "43092892", 
     "full_name": "Sophiaaa." 
     }, { 
     "username": "amna7861", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_175807260_75sq_1343135903.jpg", 
     "id": "175807260", 
     "full_name": "Amna Haroon" 
     }, { 
     "username": "yaya0318", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_74056_75sq_1287001004.jpg", 
     "id": "74056", 
     "full_name": "Mao Yaya" 
     }, { 
     "username": "jay_damage", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_197465040_75sq_1342932411.jpg", 
     "id": "197465040", 
     "full_name": "jay_damage" 
     }, { 
     "username": "reves", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_671833_75sq_1335966794.jpg", 
     "id": "671833", 
     "full_name": "Fernando D. Ramirez" 
     }, { 
     "username": "lizray1", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_198450407_75sq_1343144120.jpg", 
     "id": "198450407", 
     "full_name": "lizray1" 
     }, { 
     "username": "alivewtheglory", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_37907416_75sq_1341561441.jpg", 
     "id": "37907416", 
     "full_name": "Marilynn C" 
     }, { 
     "username": "mnforever55", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_29255977_75sq_1334833008.jpg", 
     "id": "29255977", 
     "full_name": "mnforever55" 
     }] 
    }, 
    "images": { 
     "low_resolution": { 
     "url": "http:\/\/distilleryimage9.s3.amazonaws.com\/beb7f896d16311e19fe21231380f3636_6.jpg", 
     "width": 306, 
     "height": 306 
     }, 
     "thumbnail": { 
     "url": "http:\/\/distilleryimage9.s3.amazonaws.com\/beb7f896d16311e19fe21231380f3636_5.jpg", 
     "width": 150, 
     "height": 150 
     }, 
     "standard_resolution": { 
     "url": "http:\/\/distilleryimage9.s3.amazonaws.com\/beb7f896d16311e19fe21231380f3636_7.jpg", 
     "width": 612, 
     "height": 612 
     } 
    }, 
    "caption": { 
     "created_time": "1342676255", 
     "text": "Happy birthday @amy !", 
     "from": { 
     "username": "kevin", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_3_75sq_1325536697.jpg", 
     "id": "3", 
     "full_name": "Kevin Systrom" 
     }, 
     "id": "238708186813567655" 
    }, 
    "user_has_liked": false, 
    "id": "238707833418289797_3", 
    "user": { 
     "username": "kevin", 
     "website": "", 
     "bio": "CEO & Co-founder of Instagram", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_3_75sq_1325536697.jpg", 
     "full_name": "Kevin Systrom", 
     "id": "3" 
    } 
    }, { 
    "attribution": null, 
    "tags": [], 
    "type": "image", 
    "location": { 
     "latitude": 38.503100608, 
     "name": "Goose & Gander", 
     "longitude": -122.468387538, 
     "id": 12059278 
    }, 
    "comments": { 
     "count": 85, 
     "data": [{ 
     "created_time": "1342555499", 
     "text": "Cheers !!! \ud83d\ude18\ud83d\ude18", 
     "from": { 
      "username": "kattiab", 
      "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_1345073_75sq_1340495505.jpg", 
      "id": "1345073", 
      "full_name": "kattia b" 
     }, 
     "id": "237695212468572732" 
     }, { 
     "created_time": "1342558279", 
     "text": "happy birthday instagram!", 
     "from": { 
      "username": "alanasayshi", 
      "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_4235095_75sq_1341960681.jpg", 
      "id": "4235095", 
      "full_name": "Alana Boy\u00e9r" 
     }, 
     "id": "237718535382504383" 
     }, { 
     "created_time": "1342567977", 
     "text": "Happy Natal Day Instagram!", 
     "from": { 
      "username": "cynrtst", 
      "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_12493918_75sq_1341538462.jpg", 
      "id": "12493918", 
      "full_name": "Cynthia L" 
     }, 
     "id": "237799888639758668" 
     }, { 
     "created_time": "1342568896", 
     "text": "Happy Birthday \ud83c\udf89\ud83c\udf89\ud83c\udf89 was it a long labour \ud83d\ude02\ud83d\ude02\ud83d\ude02", 
     "from": { 
      "username": "relzie", 
      "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_15718874_75sq_1332290975.jpg", 
      "id": "15718874", 
      "full_name": "Relz" 
     }, 
     "id": "237807595966960050" 
     }, { 
     "created_time": "1342579289", 
     "text": "Cheers @kevin and Happy Birthday #instagram thank you so much Kevin for creating instagram it's truly got me back out there taking more photos and falling in love with photography all over again...", 
     "from": { 
      "username": "bpphotographs", 
      "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_12149171_75sq_1339457436.jpg", 
      "id": "12149171", 
      "full_name": "bpphotographs" 
     }, 
     "id": "237894779172557723" 
     }, { 
     "created_time": "1342652660", 
     "text": "@suz_h", 
     "from": { 
      "username": "ianyorke", 
      "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_1041088_75sq_1339844137.jpg", 
      "id": "1041088", 
      "full_name": "Ian Yorke" 
     }, 
     "id": "238510264889118973" 
     }, { 
     "created_time": "1342667574", 
     "text": "Love your app\ud83d\udc97\ud83d\udc97\ud83d\udc97\ud83d\udc97\ud83d\udc97", 
     "from": { 
      "username": "gothangel1997", 
      "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_186799125_75sq_1342668825.jpg", 
      "id": "186799125", 
      "full_name": "Angel Mercado" 
     }, 
     "id": "238635368570687940" 
     }, { 
     "created_time": "1342843274", 
     "text": "\ud83d\ude09\u2764", 
     "from": { 
      "username": "andescu", 
      "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_145554839_75sq_1337641309.jpg", 
      "id": "145554839", 
      "full_name": "andescu" 
     }, 
     "id": "240109245637333685" 
     }] 
    }, 
    "filter": "Sierra", 
    "created_time": "1342332400", 
    "link": "http:\/\/instagr.am\/p\/NF0G6bABA2\/", 
    "likes": { 
     "count": 3282, 
     "data": [{ 
     "username": "caysondesigns", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_23464609_75sq_1329177054.jpg", 
     "id": "23464609", 
     "full_name": "Jasmine at Cayson Designs" 
     }, { 
     "username": "m_azooz16", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_198179404_75sq_1343096096.jpg", 
     "id": "198179404", 
     "full_name": "m_azooz16" 
     }, { 
     "username": "shulinghuang", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_144887495_75sq_1342460246.jpg", 
     "id": "144887495", 
     "full_name": "H\u3002" 
     }, { 
     "username": "caitlyn_hammonds", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/anonymousUser.jpg", 
     "id": "198322184", 
     "full_name": "caitlyn_hammonds" 
     }, { 
     "username": "sophiafrancis", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_43092892_75sq_1340548333.jpg", 
     "id": "43092892", 
     "full_name": "Sophiaaa." 
     }, { 
     "username": "beatle1234", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/anonymousUser.jpg", 
     "id": "197988834", 
     "full_name": "beatle1234" 
     }, { 
     "username": "yaya0318", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_74056_75sq_1287001004.jpg", 
     "id": "74056", 
     "full_name": "Mao Yaya" 
     }, { 
     "username": "lizray1", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_198450407_75sq_1343144120.jpg", 
     "id": "198450407", 
     "full_name": "lizray1" 
     }, { 
     "username": "rawr1234321", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_198492630_75sq_1343151765.jpg", 
     "id": "198492630", 
     "full_name": "rawr1234321" 
     }] 
    }, 
    "images": { 
     "low_resolution": { 
     "url": "http:\/\/distilleryimage6.s3.amazonaws.com\/3ec59e18ce4311e1b8031231380702ee_6.jpg", 
     "width": 306, 
     "height": 306 
     }, 
     "thumbnail": { 
     "url": "http:\/\/distilleryimage6.s3.amazonaws.com\/3ec59e18ce4311e1b8031231380702ee_5.jpg", 
     "width": 150, 
     "height": 150 
     }, 
     "standard_resolution": { 
     "url": "http:\/\/distilleryimage6.s3.amazonaws.com\/3ec59e18ce4311e1b8031231380702ee_7.jpg", 
     "width": 612, 
     "height": 612 
     } 
    }, 
    "caption": { 
     "created_time": "1342332465", 
     "text": "Mellivora capensis - eagle rare, peat, honey, lemon, pineapple, black cardamom, chili, coconut foam", 
     "from": { 
     "username": "kevin", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_3_75sq_1325536697.jpg", 
     "id": "3", 
     "full_name": "Kevin Systrom" 
     }, 
     "id": "235824269324456712" 
    }, 
    "user_has_liked": false, 
    "id": "235823728972271670_3", 
    "user": { 
     "username": "kevin", 
     "website": "", 
     "bio": "CEO & Co-founder of Instagram", 
     "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_3_75sq_1325536697.jpg", 
     "full_name": "Kevin Systrom", 
     "id": "3" 
    } 
    }, ..... 

은 그래서 개별적으로 각 게시물을 반복하고 tags, idimage urls를 추출하기 위해 찾고 있어요. 나는 (PHP 개발자이기 때문에 초보자로서 파이썬을 사용하기가 정말 어렵다.) 약간의 문제가있다.

다음은 각 게시물을 반복하고 제공된 속성을 처리하는 데 사용하는 코드입니다. 나는 목록에 그들을 저장하거나 dict 싶지 않아. 태그를 검색하고 싶을뿐입니다.

나는이 foreach와 함께이 쉽게 사용하여 PHP를 할 수 있었다

info= simplejson.load(info) 
    print type(info['data']) # I get it as a list 
    for k, v in info['data']: 
     print v 

(내가 사용해야하는 루프 발견을 couldnt 때문에이 그냥 시도 코드) : info['data']

foreach($info->data as $i) { 
    $tags = $i->tags(); 
    $id = $i->id(); 
} 
+0

PHP의'foreach..as'는 파이썬'for..in'과 어떤 점에서 다른가요? –

+0

"ValueError : 포장을 푸는 데 너무 많은 값" – Vanthewilderperson

+0

'k, v'를 사용하면 전체'post' 개체를 두 값으로 압축하려고 시도하는데 작동하지 않습니다. –

답변

0

경우 목록을 통해 다음과 같이 반복 할 수 있습니다.

for post in info['data']: 
    tags = post['tags'] 
    id = post['id'] 
    image_urls = [] # An empty list -- we'll fill it below 
    for img_type in ['low_resolution', 'thumbnail', 'standard_resolution']: 
     image_urls.append(post['images'][img_type]['url']) 
    # Now image_urls has all the image urls in it 

PHP와는 다소 다른 부분은 JSON 구조에서 키가 "tags" 인 곳이라면 파이썬에서 "tags" 문자열을 사용해야하며 PHP에서는 리터럴 tags()을 사용합니다.

+0

그것은 쉬웠다! 감사합니다 :) – Vanthewilderperson

+0

괜찮습니다! 파이썬을 배우십시오 - 제가 가장 좋아하는 프로그래밍 언어입니다. :-) –

관련 문제