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 数据的主要顶级数据结构只有一个 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 解析器。例如,如果我将您提供的数据网页保存到一个名为 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 回答