Python Json解析输出

turtle02

我是json解析的新手,但我想从此json响应中获取salePrice,名称,型号。响应似乎全都在1行上,我没有运气来获取数据。

`{u'sort':u'relevance',u'items':[{u'marketplace':False,u'imageEntities':[{u'entityType':u'SECONDARY',u'thumbnailImage':u' http://i5.walmartimages.com/dfw/dce07b8c-b23c/k2-_69d110a4-cba1-4d8b-a1c5-3892ef5b4b43.v1.jpg ”,u'largeImage ':U' http://i5.walmartimages.com/ DFW / dce07b8c-3397 / K2-_ae913276-674a-4ef3-9e74-b53a586c294d.v1.jpg ”,u'mediumImage ':U' http://i5.walmartimages.com/dfw/dce07b8c-bc42/k2-_86541363- b442-468e-8601-88273996cd30.v1.jpg '},{u'entityType':u'SECONDARY”,u'thumbnailImage ':U' http://i5.walmartimages.com/dfw/dce07b8c-30ec/k2- _e470778f-a046-452a-9436-8d77ad484bbe.v1.jpg ',u'largeImage':u'http: //i5.walmartimages.com/dfw/dce07b8c-3661/k2-_54ec03e5-52ed-40ff-ae67-e25cfceff840.v1.jpg”,u'mediumImage ':U' http://i5.walmartimages.com/dfw/dce07b8c-98ea/k2-_08fa39a0-73cf-444f-9c83-30ddff3b5d61.v1.jpg '},{u'entityType':U '次要',u'thumbnailImage ':U' http://i5.walmartimages.com/dfw/dce07b8c-ea17/k2-_ec99e602-ae9e-4fb7-ba9d-1f00d1c068fa.v1.jpg 'u'largeImage':U ' http://i5.walmartimages.com/dfw/dce07b8c-b56f/k2-_12d4e3c1-454f-4fb0-9036-e72f7efca220.v1.jpg ',u'mediumImage ':U' http://i5.walmartimages.com /dfw/dce07b8c-c5ea/k2-_4b471514-753d-47bf-a82e-f8398aa7dda5.v1.jpg '},{u'entityType':u'SECONDARY”,u'thumbnailImage ':U' HTTP://i5.walmartimages .com / dfw / dce07b8c-1ba6 / k2-_55347198-badb-47fc-ba68-d77973bf9a6d.v1。jpg ',u'largeImage':u'http://i5.walmartimages.com/dfw/dce07b8c-72fc/k2-_90b99fa7-4016-40e4-8998-7ea93ea30e98.v1.jpg ”,u'mediumImage ':U' http://i5.walmartimages.com/ dfw / dce07b8c-5d76 / k2-_4051f745-f43f-48b3-86b6-f82d9e999fca.v1.jpg'Traceback(最近一次通话):},{u'entityType':u'SECONDARY',u'thumbnailImage':u' http://i5.walmartimages.com/dfw/dce07b8c-b0d0/k2-_526f7d4b-212a-4f60-b090-116f481a132b.v1.jpg ”,u'largeImage ':U' http://i5.walmartimages.com/ DFW / dce07b8c-A419 / K2-_d986683e-46f1-4c58-8594-cb1ccd3cfdb5.v1.jpg ”,u'mediumImage ':U' http://i5.walmartimages.com/dfw/dce07b8c-cafc/k2-_91678e98- 52e5-4461-8587-0007f84dee24.v1.jpg '},{u'entityType':u'PRIMARY',u'thumbnailImage':u'http://i5.walmartimages.com/dfw/dce07b8c-4cc9/k2-_8a40e4a9-ae3c-4df7-a9fc-71b48f3a7970.v1.jpg ”,u'largeImage ':U' http://i5.walmartimages.com/ DFW / dce07b8c-f9fe / K2-_e5b07842-8a7f-4de3-A212-ce36b44416fa.v1.jpg ”,u'mediumImage ':U' http://i5.walmartimages.com/dfw/dce07b8c-a2c0/k2-_2e9d5b96- eadb-45f0-8c56-9d04adc941fa.v1.jpg '}],u'numReviews':4,u'productUrl':u'http://c.affil.walmart.com/t/api02?l= http% 3A %2F%2Fwww.walmart.com%2Fip%2FThe恶中之的Xbox一体%2F26096326%3Faffp1%3Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi ”,u'customerRatingImage ':U' HTTP ://i2.walmartimages.com/i/CustRating/4_8.gif',u 'addToCartUrl':u'http://c.affil.walmart.com/t/api02?l=http%3A%2F%2Faffil.walmart.com%2Fcart%2FaddToCart%3Fitems%3D26096326%7C1%26affp1%3Dtj_EvYKs1pUCyepapiYaffS%7% 26veh%3Daff%26wmlspartner%3Dreadonlyapi他发现自己处于一个荒诞的世界中,可怕的生物在死者中游荡。面对不可思议的恐怖并为生存而战,塞巴斯蒂安踏上了一段令人恐惧的旅程,以揭开这股邪恶力量的根源。“,u'customerRating':u'4.75',u'affiliateAddToCartUrl':u'http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1082&RD_PARM1=http%253A%252F%252Faffil.walmart.com%252Fcart% 252FaddToCart%253Fitems%253D26096326%257C1%2526affp1%253Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%2526affilsrc%253Dapi ”,u'mediumImage ':U' http://i5.walmartimages.com/dfw/dce07b8c-a2c0/k2-_2e9d5b96-eadb-45f0- 8c56-9d04adc941fa.v1.jpg ”,u'productTrackingUrl ':U' http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid= 1082&RD_PARM1 = http%253A%252F%252Fwww.walmart。com%252Fip%252FThe-Evil-Within-Xbox-One%252F26096326%253Faffp1%253Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%2526affilsrc%253Dapi',u'stock':u'Available',u'categoryNode':u'2636_1040579',u'giftOptions':{u'allowGiftWrap':False,u'allowGiftMessage':False,u'allowGiftReceipt':False}, u'largeImage ':U' http://i5.walmartimages.com/dfw/dce07b8c-f9fe/k2-_e5b07842-8a7f-4de3-a212-ce36b44416fa.v1.jpg 'u'parentItemId':26096326,u'thumbnailImage ':u'http : //i5.walmartimages.com/dfw/dce07b8c-4cc9/k2-_8a40e4a9-ae3c-4df7-a9fc-71b48f3a7970.v1.jpg不确定世界中的未知威胁-在一个曲折曲折的世界中,神秘而邪恶的恐惧即将来临。走廊,墙壁,门和整座建筑物实时变化,使玩家陷入随时可能从任何方向出现威胁的现实。</ li> <li>恐怖的新面孔-精心制作的环境,令人恐惧的焦虑和错综复杂的故事交织在一起,创造了一个身临其境的世界,它将带给玩家紧张的高度。</ li> <li> ESRB评分:血腥血腥,强大语言和强烈暴力的成熟度</ li> </ ul>”,u'standardShipRate' :4.97,u'categoryPath':u'视频游戏/视频游戏',u'itemId':26096326,u'name':

这是我的代码。

import json
import requests


def getPrice(request):
    try:
        price = request.find('"49.96"')
        print price
        return price
    except: 
        print'fail Price'

def getData():
    request = requests.get('http://api.walmartlabs.com/v1/search?query=093155118539&format=json&apiKey=KEYSHOULDGOHERE')
    data = request.json()
    print data #where I get my output
    price = getPrice(request)

getData()

链接到api doc-我没有提供废弃的API密钥。https://developer.walmartlabs.com/io-docs

qvpham

您需要知道响应的格式以对其进行解析。我看到了,你用过的requests.get()

收到请求后

request = requests.get(...)

request.json()将返回JSON响应的dict对象。看起来像这样(格式化后,使用了@Alex这样的代码):

{u'sort': u'relevance', u'items': [{u'marketplace': False,
                                        u'imageEntities': [
                                            {u'entityType': u'SECONDARY',
                                             u'thumbnailImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-b23c/k2-_69d110a4-cba1-4d8b-a1c5-3892ef5b4b43.v1.jpg',
                                             u'largeImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-3397/k2-_ae913276-674a-4ef3-9e74-b53a586c294d.v1.jpg',
                                             u'mediumImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-bc42/k2-_86541363-b442-468e-8601-88273996cd30.v1.jpg'},
                                            {u'entityType': u'SECONDARY',
                                             u'thumbnailImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-30ec/k2-_e470778f-a046-452a-9436-8d77ad484bbe.v1.jpg',
                                             u'largeImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-3661/k2-_54ec03e5-52ed-40ff-ae67-e25cfceff840.v1.jpg',
                                             u'mediumImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-98ea/k2-_08fa39a0-73cf-444f-9c83-30ddff3b5d61.v1.jpg'},
                                            {u'entityType': u'SECONDARY',
                                             u'thumbnailImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-ea17/k2-_ec99e602-ae9e-4fb7-ba9d-1f00d1c068fa.v1.jpg',
                                             u'largeImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-b56f/k2-_12d4e3c1-454f-4fb0-9036-e72f7efca220.v1.jpg',
                                             u'mediumImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-c5ea/k2-_4b471514-753d-47bf-a82e-f8398aa7dda5.v1.jpg'},
                                            {u'entityType': u'SECONDARY',
                                             u'thumbnailImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-1ba6/k2-_55347198-badb-47fc-ba68-d77973bf9a6d.v1.jpg',
                                             u'largeImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-72fc/k2-_90b99fa7-4016-40e4-8998-7ea93ea30e98.v1.jpg',
                                             u'mediumImage':
                                                 u'http://i5.walmartimages.com/dfw/dce07b8c-5d76/k2-_4051f745-f43f-48b3-86b6-f82d9e999fca.v1.jpg'
                                             }, {
                                                u'entityType': u'SECONDARY',
                                                u'thumbnailImage':
                                                    u'http://i5.walmartimages.com/dfw/dce07b8c-b0d0/k2-_526f7d4b-212a-4f60-b090-116f481a132b.v1.jpg',
                                                u'largeImage':
                                                    u'http://i5.walmartimages.com/dfw/dce07b8c-a419/k2-_d986683e-46f1-4c58-8594-cb1ccd3cfdb5.v1.jpg',
                                                u'mediumImage':
                                                    u'http://i5.walmartimages.com/dfw/dce07b8c-cafc/k2-_91678e98-52e5-4461-8587-0007f84dee24.v1.jpg'},
                                            {You have did that
                                                u'entityType': u'PRIMARY',
                                                u'thumbnailImage':
                                                    u'http://i5.walmartimages.com/dfw/dce07b8c-4cc9/k2-_8a40e4a9-ae3c-4df7-a9fc-71b48f3a7970.v1.jpg',
                                                u'largeImage':
                                                    u'http://i5.walmartimages.com/dfw/dce07b8c-f9fe/k2-_e5b07842-8a7f-4de3-a212-ce36b44416fa.v1.jpg',
                                                u'mediumImage':
                                                    u'http://i5.walmartimages.com/dfw/dce07b8c-a2c0/k2-_2e9d5b96-eadb-45f0-8c56-9d04adc941fa.v1.jpg'}],
                                        u'numReviews': 4,
                                        u'productUrl':
                                            u'http://c.affil.walmart.com/t/api02?l=http%3A%2F%2Fwww.walmart.com%2Fip%2FThe-Evil-Within-Xbox-One%2F26096326%3Faffp1%3Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi',
                                        u'customerRatingImage':
                                            u'http://i2.walmartimages.com/i/CustRating/4_8.gif',
                                        u'addToCartUrl':
                                            u'http://c.affil.walmart.com/t/api02?l=http%3A%2F%2Faffil.walmart.com%2Fcart%2FaddToCart%3Fitems%3D26096326%7C1%26affp1%3Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi',
                                        u'shortDescription': u"Developed by "
                                                             u"Shinji Mikami "
                                                             u"- creator of "
                                                             u"the seminal "
                                                             u"Resident Evil "
                                                             u"series - and "
                                                             u"the talented "
                                                             u"team at Tango "
                                                             u"Gameworks, "
                                                             u"The Evil "
                                                             u"Within "
                                                             u"embodies the "
                                                             u"meaning of "
                                                             u"pure survival "
                                                             u"horror. "
                                                             u"Experience a "
                                                             u"disturbing "
                                                             u"reality as "
                                                             u"you try to "
                                                             u"break free "
                                                             u"from warped "
                                                             u"machinations "
                                                             u"that could "
                                                             u"only exist in "
                                                             u"the most "
                                                             u"horrifying "
                                                             u"worlds. Face "
                                                             u"twisted "
                                                             u"creatures and "
                                                             u"experience "
                                                             u"true terror, "
                                                             u"all "
                                                             u"heightened by "
                                                             u"cutting edge "
                                                             u"lighting and "
                                                             u"animation "
                                                             u"made possible "
                                                             u"by a "
                                                             u"customized "
                                                             u"version of "
                                                             u"the id Tech 5 "
                                                             u"engine. While "
                                                             u"investigating "
                                                             u"the scene of "
                                                             u"a gruesome "
                                                             u"mass murder, "
                                                             u"Detective "
                                                             u"Sebastian "
                                                             u"Castellanos "
                                                             u"and his "
                                                             u"partners "
                                                             u"encounter a "
                                                             u"mysterious "
                                                             u"and powerful "
                                                             u"force. After "
                                                             u"seeing the "
                                                             u"slaughter of "
                                                             u"fellow "
                                                             u"officers, "
                                                             u"Sebastian is "
                                                             u"ambushed and "
                                                             u"knocked "
                                                             u"unconscious. "
                                                             u"When he "
                                                             u"awakens, "
                                                             u"he finds "
                                                             u"himself in a "
                                                             u"deranged "
                                                             u"world where "
                                                             u"hideous "
                                                             u"creatures "
                                                        salePrice,name,modelnumber     u"wander among "
                                                             u"the dead. "
                                                             u"Facing "
                                                             u"unimaginable "
                                                             u"terror, "
                                                             u"and fighting "
                                                             u"for survival, "
                                                             u"Sebastian "
                                                             u"embarks on a "
                                                             u"frightening "
                                                             u"journey to "
                                                             u"unravel "
                                                             u"what's behind "
                                                             u"this evil "
                                                             u"force.",
                                        u'customerRating': u'4.75',
                                        u'affiliateAddToCartUrl':
                                            u'http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1082&RD_PARM1=http%253A%252F%252Faffil.walmart.com%252Fcart%252FaddToCart%253Fitems%253D26096326%257C1%2526affp1%253Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%2526affilsrc%253Dapi',
                                        u'mediumImage':
                                            salePrice,name,modelnumbersalePrice,name,modelnumberu'http://i5.walmartimages.com/dfw/dce07b8c-a2c0/k2-_2e9d5b96-eadb-45f0-8c56-9d04adc941fa.v1.jpg',
                                        u'productTrackingUrl':
                                            u'http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1082&RD_PARM1=http%253A%252F%252Fwww.walmart.com%252Fip%252FThe-Evil-Within-Xbox-One%252F26096326%253Faffp1%253Dtj_EvYKs1pUCYEvoiflaNgxllgazmJGF-p7licrcSzU%2526affilsrc%253Dapi',
                                        u'stock': u'Available',
                                        u'categoryNode': u'2636_1040579',
                                        u'giftOptions': {
                                            u'allowGiftWrap': False,
                                            u'allowGiftMessage': False,
                                            u'allowGiftReceipt': False},
                                        u'largeImage':
                                            u'http://i5.walmartimages.com/dfw/dce07b8c-f9fe/k2-_e5b07842-8a7f-4de3-a212-ce36b44416fa.v1.jpg',
                                        u'parentItemId': 26096326,
                                        u'thumbnailImage':
                                            u'http://i5.walmartimages.com/dfw/dce07b8c-4cc9/k2-_8a40e4a9-ae3c-4df7-a9fc-71b48f3a7970.v1.jpg',
                                        u'bundle': False,
                                        u'longDescription':
                                            u"&lt;ul&gt;&lt;li&gt;Pure "
                                            u"Survival Horror Returns - "
                                            u"Shinji Mikami, the father of "
                                            u"survival horror, is back to "
                                            u"direct a chilling new game "
                                            u"wrapped in haunting narrative. "
                                            u"Tension and anxiety heighten "
                                            u"dramatically as you explore "
                                            u"the game's tortured world. "
                                            u"You'll have limited resources "
                                            u"at your disposal as you fight "
                                            u"for your survival and "
                                            u"experience profound fear in "
                                            u"this perfect blend of horror "
                                            u"and "
                                            u"action.&lt;/li&gt;&lt;li&gt;Brutal Traps and Twisted Creatures - Face unthinkable horrors and cruel traps as you struggle to survive against overwhelming odds. Turn evil against itself by using the same diabolical devices against overwhelming deadly creatures.&lt;/li&gt;&lt;li&gt;Unknown Threats in an Uncertain World - Mysterious and wicked fears loom ahead in a world that warps and twists around you. Corridors, walls, doors, and entire buildings change in real-time, ensnaring players in a reality where threats can appear at any time and from any direction.&lt;/li&gt;&lt;li&gt;The New Face of Horror - Highly-crafted environments, horrifying anxiety, and an intricate story weave together to create an immersive world that will bring players to the height of tension. Defeat insurmountable terror and experience the ultimate thrill by discovering The Evil Within.&lt;/li&gt;&lt;li&gt;ESRB Rating: MATURE with Blood and Gore, Strong Language, and Intense Violence&lt;/li&gt;&lt;/ul&gt;",
                                        u'standardShipRate': 4.97,
                                        u'categoryPath': u'Video Games/Video '
                                                         u'Games',
                                        u'itemId': 26096326,
                                        u'name': u'The Evil Within (Xbox One)',
                                        u'upc': u'093155118539',
                                        u'modelNumber': u'093155118539',
                                        u'salePrice': 49.96,
                                        u'availableOnline': True,
                                        u'msrp': 59.99}], u'facets': [],
     u'start': 1, u'numItems': 1, u'query': u'093155118539',
     u'totalResults': 1, u'responseGroup': u'base'}

您看到的u'items'是项目的列表对象。的每个元素items都是项目的dict对象。->在u'items'您的每个元素中都会找到salePrice,name,modelNumber

注意:是modelNumber,不是modelnumber

fields_2_get = [u'salePrice',u'name',u'modelNumber']
data = response.json()
for item in data[u'items']:
   for field_name in fields_2_get:
      print(field + ": " + str(item.get(field_name, None))) 

您的request对象没有功能find()这样request.find("xxx")就行不通了。

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章