3

私は Python 2.7 を使用しています。
私がやろうとしていることは次のとおりです。次の JSON 文字列があります。

http://www.ows.newegg.com/Products.egg/N82E16822148992

この文字列には、 と呼ばれる複数の値がありますFinalPrice。問題のアイテムの を取得しようとしてMAIN FinalPriceいます (ここのアイテムは です#N82E16822148992)。はmain FinalPrice、ページのメイン アイテムを参照するものです。この JSON ページに対応するページはhttp://www.newegg.com/Product/Product.aspx?Item=N82E16822148992.

FinalPriceそれで、通常約10から必要なものを取得する方法はありますかFinalPrices(私は通常、FinalPrices3つのリンクでの数をテストしただけで、それらはすべて10であるためです)?製品名を使用して最終価格の場所を見つける方法を見つけることができるのではないかと思いましたが、製品FinalPrice.

これについて何ができますか?

PS これが私が試したことです:
私は Python.org の JSON ドキュメントを見てきましたが、特定の FinalPrice を見つけるのにあまり役に立ちませんでした (または、単に間違って見ていましたか?)
JSON ページ全体が文字列であり、文字列操作 (s.find、s[a:b]) を使用して必要な FinalPrice を取得しましたが、最初のもの以外に FinalPrice を取得する方法が見つかりませんでした。
これに対する適切な答えをGoogleとStack Overflowで検索しました。

私が今までやってきたのはこれくらいです。

4

2 に答える 2

3

リンクした JSON データのメインの最上位データ構造にはキーが1 つ FinalPriceしかないため、この情報を取得するのは簡単です。

>>> import urllib2, json
>>> data = json.load(urllib2.urlopen('http://www.ows.newegg.com/Products.egg/N82E16822148992'))
>>> print data[u'FinalPrice']
$79.99

pprint.pprintこの関数を使用して、データ構造を簡単に把握できます。

>>> from pprint import pprint
>>> pprint(data)
{u'AddToCartButtonText': u'Add to Cart',
 u'AddToCartButtonType': u'AD',
 u'AverageRating': 0,
 u'BrandInfo': None,
 u'CanAddToCart': False,
 u'ComboCount': 5,
 u'CoremetricsInfo': {u'Brand': u'Seagate',
                      u'CategoryID': u'IPS380',
                      u'PageID': u'PRODUCT: NBHD 320G|ST ST320LT014 7K SATA3G %_22-148-992 (22-148-992)',
                      u'ProductName': u'NBHD 320G|ST ST320LT014 7K SATA3G %_22-148-992'},
 u'Discount': u'You Save: $10.00',
 u'ETA': u'/Date(-62135568000000)/',
 u'EmailFriendImageInfo': {u'FullPath': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S35$',
                           u'ItemNumber': None,
                           u'PathSize100': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S100$',
                           u'PathSize125': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S125$',
                           u'PathSize180': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S180$',
                           u'PathSize300': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S300$',
                           u'PathSize35': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S35$',
                           u'PathSize60': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S60$',
                           u'PathSize640': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S640$',
                           u'SmallImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S125$',
                           u'ThumbnailImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S60$',
                           u'Title': None},
 u'EnergyStarText': None,
 u'FinalPrice': u'$79.99',
 u'FreeShippingFlag': True,
 u'HasMappingPrice': False,
 u'Image': {u'FullPath': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S180$',
            u'ItemNumber': None,
            u'PathSize100': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S100$',
            u'PathSize125': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S125$',
            u'PathSize180': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S180$',
            u'PathSize300': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S300$',
            u'PathSize35': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S35$',
            u'PathSize60': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S60$',
            u'PathSize640': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S640$',
            u'SmallImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S100$',
            u'ThumbnailImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S60$',
            u'Title': None},
 u'InstantSaving': 0,
 u'Instock': True,
 u'InstockForCombo': False,
 u'IronEggDescription': None,
 u'IsActivated': True,
 u'IsCellPhoneItem': False,
 u'IsComboBundle': False,
 u'IsFeaturedItem': False,
 u'IsHot': False,
 u'IsInPMCC': False,
 u'IsPreLaunch': False,
 u'IsShellShockerItem': False,
 u'IsShipByNewegg': True,
 u'IsShowEnergyStarSection': False,
 u'IsShowSoldOutText': False,
 u'ItemGroupID': 0,
 u'ItemMapPriceMarkType': 0,
 u'ItemNumber': u'22-148-992',
 u'ItemOwnerType': 0,
 u'LimitQuantity': -1,
 u'MailInRebateInfo': None,
 u'MailInRebateText': None,
 u'MappingFinalPrice': None,
 u'Model': u'ST320LT014',
 u'NeweggItemNumber': None,
 u'NumberOfReviews': 0,
 u'OriginalPrice': u'$89.99',
 u'ParentItem': None,
 u'ProductProperties': None,
 u'ProductStockType': 0,
 u'PromotionInfo': None,
 u'PromotionText': u'',
 u'ReturnPolicyInfo': {u'HtmlContent': None,
                       u'ID': u'44',
                       u'Name': u'Standard Return Policy'},
 u'ReviewSummary': {u'Rating': 0, u'TotalReviews': u'[]'},
 u'SellerId': None,
 u'SellerItemPropertyList': None,
 u'SellerName': None,
 u'ShipByNewegg': 0,
 u'ShippingInfo': {u'NormalShippingText': u'Free Shipping*',
                   u'RestrictedShippingText': u' Additional fees may apply for shipments to APO/FPO, AK, HI and PR.',
                   u'RestrictedShippingTitle': u'Shipping Restrictions',
                   u'SpecialShippingText': u' Additional fees may apply for shipments to APO/FPO, AK, HI and PR.'},
 u'ShippingPromotionInfo': None,
 u'ShoppingInsight': {u'Description': u'See what other informed Newegg customers purchased after viewing this product',
                      u'PromotionItems': [{u'AfterRebate': None,
                                           u'FinalPrice': u'$149.99',
                                           u'Instock': True,
                                           u'IsCurrentItem': False,
                                           u'IsFreeShipping': True,
                                           u'IsShowOriginalPrice': False,
                                           u'ItemBrand': {u'BrandId': 1305,
                                                          u'BrandImage': u'http://images10.newegg.com/brandimage/Brand1305.gif',
                                                          u'Code': 0,
                                                          u'Description': u'Seagate',
                                                          u'HasManfactoryLogo': True,
                                                          u'ManufactoryWeb': u'http://www.seagate.com',
                                                          u'WebSiteURL': None},
                                           u'ItemImage': {u'FullPath': u'http://images17.newegg.com/is/image/newegg/22-148-591-TS?$S180W$',
                                                          u'ItemNumber': None,
                                                          u'PathSize100': u'http://images17.newegg.com/is/image/newegg/22-148-591-TS?$S100$',
                                                          u'PathSize125': u'http://images17.newegg.com/is/image/newegg/22-148-591-TS?$S125W$',
                                                          u'PathSize180': u'http://images17.newegg.com/is/image/newegg/22-148-591-TS?$S180W$',
                                                          u'PathSize300': u'http://images17.newegg.com/is/image/newegg/22-148-591-TS?$S300W$',
                                                          u'PathSize35': u'http://images17.newegg.com/is/image/newegg/22-148-591-TS?$S35$',
                                                          u'PathSize60': u'http://images17.newegg.com/is/image/newegg/22-148-591-TS?$S60$',
                                                          u'PathSize640': u'http://images17.newegg.com/is/image/newegg/22-148-591-TS?$S640W$',
                                                          u'SmallImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-591-TS?$S125W$',
                                                          u'ThumbnailImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-591-TS?$S60$',
                                                          u'Title': None},
                                           u'ItemMapPriceMarkType': 0,
                                           u'ItemNumber': u'22-148-591',
                                           u'MappingFinalPrice': None,
                                           u'OriginalPrice': u'$149.99',
                                           u'Percentage': u'22',
                                           u'ReviewSummary': {u'Rating': 4,
                                                              u'TotalReviews': u'[829]'},
                                           u'Title': u'Seagate Momentus XT ST95005620AS 500GB 7200 RPM 32MB Cache 2.5" SATA 3.0Gb/s with NCQ Solid State Hybrid Drive -Bare Drive'},
                                          {u'AfterRebate': None,
                                           u'FinalPrice': u'$79.99',
                                           u'Instock': True,
                                           u'IsCurrentItem': True,
                                           u'IsFreeShipping': True,
                                           u'IsShowOriginalPrice': True,
                                           u'ItemBrand': {u'BrandId': 1305,
                                                          u'BrandImage': u'http://images10.newegg.com/brandimage/Brand1305.gif',
                                                          u'Code': 0,
                                                          u'Description': u'Seagate',
                                                          u'HasManfactoryLogo': True,
                                                          u'ManufactoryWeb': u'http://www.seagate.com',
                                                          u'WebSiteURL': None},
                                           u'ItemImage': {u'FullPath': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S180$',
                                                          u'ItemNumber': None,
                                                          u'PathSize100': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S100$',
                                                          u'PathSize125': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S125$',
                                                          u'PathSize180': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S180$',
                                                          u'PathSize300': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S300$',
                                                          u'PathSize35': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S35$',
                                                          u'PathSize60': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S60$',
                                                          u'PathSize640': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S640$',
                                                          u'SmallImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S125$',
                                                          u'ThumbnailImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-992-TS?$S60$',
                                                          u'Title': None},
                                           u'ItemMapPriceMarkType': 0,
                                           u'ItemNumber': u'22-148-992',
                                           u'MappingFinalPrice': None,
                                           u'OriginalPrice': u'$89.99',
                                           u'Percentage': u'16',
                                           u'ReviewSummary': {u'Rating': 0,
                                                              u'TotalReviews': u'[]'},
                                           u'Title': u'Seagate Momentus Thin ST320LT014 320GB 7200 RPM 16MB Cache 2.5" SATA 3.0Gb/s Internal Notebook Hard Drive -Bare Drive'},
                                          {u'AfterRebate': None,
                                           u'FinalPrice': u'$69.99',
                                           u'Instock': True,
                                           u'IsCurrentItem': False,
                                           u'IsFreeShipping': False,
                                           u'IsShowOriginalPrice': True,
                                           u'ItemBrand': {u'BrandId': 1306,
                                                          u'BrandImage': u'http://images10.newegg.com/brandimage/Brand1306.gif',
                                                          u'Code': 0,
                                                          u'Description': u'Western Digital',
                                                          u'HasManfactoryLogo': True,
                                                          u'ManufactoryWeb': u'http://www.wdc.com/en/',
                                                          u'WebSiteURL': None},
                                           u'ItemImage': {u'FullPath': u'http://images17.newegg.com/is/image/newegg/22-136-280-TS?$S180W$',
                                                          u'ItemNumber': None,
                                                          u'PathSize100': u'http://images17.newegg.com/is/image/newegg/22-136-280-TS?$S100$',
                                                          u'PathSize125': u'http://images17.newegg.com/is/image/newegg/22-136-280-TS?$S125W$',
                                                          u'PathSize180': u'http://images17.newegg.com/is/image/newegg/22-136-280-TS?$S180W$',
                                                          u'PathSize300': u'http://images17.newegg.com/is/image/newegg/22-136-280-TS?$S300W$',
                                                          u'PathSize35': u'http://images17.newegg.com/is/image/newegg/22-136-280-TS?$S35$',
                                                          u'PathSize60': u'http://images17.newegg.com/is/image/newegg/22-136-280-TS?$S60$',
                                                          u'PathSize640': u'http://images17.newegg.com/is/image/newegg/22-136-280-TS?$S640W$',
                                                          u'SmallImagePath': u'http://images17.newegg.com/is/image/newegg/22-136-280-TS?$S125W$',
                                                          u'ThumbnailImagePath': u'http://images17.newegg.com/is/image/newegg/22-136-280-TS?$S60$',
                                                          u'Title': None},
                                           u'ItemMapPriceMarkType': 0,
                                           u'ItemNumber': u'22-136-280',
                                           u'MappingFinalPrice': None,
                                           u'OriginalPrice': u'$89.99',
                                           u'Percentage': u'16',
                                           u'ReviewSummary': {u'Rating': 5,
                                                              u'TotalReviews': u'[1,299]'},
                                           u'Title': u'Western Digital Scorpio Black WD3200BEKT 320GB 7200 RPM 16MB Cache 2.5" SATA 3.0Gb/s Internal Notebook Hard Drive -Bare Drive'},
                                          {u'AfterRebate': None,
                                           u'FinalPrice': u'$59.99',
                                           u'Instock': True,
                                           u'IsCurrentItem': False,
                                           u'IsFreeShipping': True,
                                           u'IsShowOriginalPrice': True,
                                           u'ItemBrand': {u'BrandId': 1305,
                                                          u'BrandImage': u'http://images10.newegg.com/brandimage/Brand1305.gif',
                                                          u'Code': 0,
                                                          u'Description': u'Seagate',
                                                          u'HasManfactoryLogo': True,
                                                          u'ManufactoryWeb': u'http://www.seagate.com',
                                                          u'WebSiteURL': None},
                                           u'ItemImage': {u'FullPath': u'http://images17.newegg.com/is/image/newegg/22-148-906-TS?$S180$',
                                                          u'ItemNumber': None,
                                                          u'PathSize100': u'http://images17.newegg.com/is/image/newegg/22-148-906-TS?$S100$',
                                                          u'PathSize125': u'http://images17.newegg.com/is/image/newegg/22-148-906-TS?$S125$',
                                                          u'PathSize180': u'http://images17.newegg.com/is/image/newegg/22-148-906-TS?$S180$',
                                                          u'PathSize300': u'http://images17.newegg.com/is/image/newegg/22-148-906-TS?$S300$',
                                                          u'PathSize35': u'http://images17.newegg.com/is/image/newegg/22-148-906-TS?$S35$',
                                                          u'PathSize60': u'http://images17.newegg.com/is/image/newegg/22-148-906-TS?$S60$',
                                                          u'PathSize640': u'http://images17.newegg.com/is/image/newegg/22-148-906-TS?$S640$',
                                                          u'SmallImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-906-TS?$S125$',
                                                          u'ThumbnailImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-906-TS?$S60$',
                                                          u'Title': None},
                                           u'ItemMapPriceMarkType': 0,
                                           u'ItemNumber': u'22-148-906',
                                           u'MappingFinalPrice': None,
                                           u'OriginalPrice': u'$69.99',
                                           u'Percentage': u'11',
                                           u'ReviewSummary': {u'Rating': 5,
                                                              u'TotalReviews': u'[1]'},
                                           u'Title': u'Seagate Momentus Thin ST250LT003 250GB 5400 RPM 16MB Cache 2.5" SATA 3.0Gb/s Internal Notebook Hard Drive -Bare Drive'}]},
 u'ShowOriginalPrice': True,
 u'StaticText': None,
 u'StrAddItem': None,
 u'StrAlt': None,
 u'StrCartImg': None,
 u'SubCategoryId': 380,
 u'SubCategoryName': u'Laptop Hard Drives',
 u'Title': u'Seagate Momentus Thin ST320LT014 320GB 7200 RPM 16MB Cache 2.5" SATA 3.0Gb/s Internal Notebook Hard Drive -Bare Drive',
 u'UnitPrice': 0,
 u'Warnings': None,
 u'WarrantyInfo': None,
 u'XmlSpec': None,
 u'imageGallery': [{u'FullPath': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z01?$S640$',
                    u'ItemNumber': None,
                    u'PathSize100': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z01?$S100$',
                    u'PathSize125': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z01?$S125$',
                    u'PathSize180': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z01?$S180$',
                    u'PathSize300': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z01?$S300$',
                    u'PathSize35': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z01?$S35$',
                    u'PathSize60': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z01?$S60$',
                    u'PathSize640': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z01?$S640$',
                    u'SmallImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z01?$S100$',
                    u'ThumbnailImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z01?$S60$',
                    u'Title': None},
                   {u'FullPath': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z02?$S640$',
                    u'ItemNumber': None,
                    u'PathSize100': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z02?$S100$',
                    u'PathSize125': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z02?$S125$',
                    u'PathSize180': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z02?$S180$',
                    u'PathSize300': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z02?$S300$',
                    u'PathSize35': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z02?$S35$',
                    u'PathSize60': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z02?$S60$',
                    u'PathSize640': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z02?$S640$',
                    u'SmallImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z02?$S100$',
                    u'ThumbnailImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z02?$S60$',
                    u'Title': None},
                   {u'FullPath': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z03?$S640$',
                    u'ItemNumber': None,
                    u'PathSize100': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z03?$S100$',
                    u'PathSize125': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z03?$S125$',
                    u'PathSize180': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z03?$S180$',
                    u'PathSize300': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z03?$S300$',
                    u'PathSize35': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z03?$S35$',
                    u'PathSize60': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z03?$S60$',
                    u'PathSize640': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z03?$S640$',
                    u'SmallImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z03?$S100$',
                    u'ThumbnailImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z03?$S60$',
                    u'Title': None},
                   {u'FullPath': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z04?$S640$',
                    u'ItemNumber': None,
                    u'PathSize100': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z04?$S100$',
                    u'PathSize125': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z04?$S125$',
                    u'PathSize180': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z04?$S180$',
                    u'PathSize300': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z04?$S300$',
                    u'PathSize35': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z04?$S35$',
                    u'PathSize60': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z04?$S60$',
                    u'PathSize640': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z04?$S640$',
                    u'SmallImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z04?$S100$',
                    u'ThumbnailImagePath': u'http://images17.newegg.com/is/image/newegg/22-148-992-Z04?$S60$',
                    u'Title': None}]}

次に、他のFinalPriceエントリがリストであるShoppingInsight->PromotionItems構造の一部であることがわかります。

>>> print data[u'ShoppingInsight'][u'PromotionItems'][0][u'FinalPrice']
$149.99
>>> print data[u'ShoppingInsight'][u'PromotionItems'][0][u'Title']
Seagate Momentus XT ST95005620AS 500GB 7200 RPM 32MB Cache 2.5" SATA 3.0Gb/s with NCQ Solid State Hybrid Drive -Bare Drive
于 2012-07-02T18:17:30.097 に答える
2

json パーサーを使用します。たとえば、提供されたデータの Web ページを json_data というファイルに保存すると、これを行うことができます...

  import json
  json_data=open('json_data')

  data = json.load(json_data)
  print(data["FinalPrice"])
  json_data.close()

または、変数に既にデータがある場合は、その変数で json.load を使用します。

于 2012-07-02T18:10:17.163 に答える