我正在使用Python2.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
那么,有没有什么方法可以从通常10个左右的链接中获取我所需要的FinalPrice(我说通常是因为我只测试了3个链接的FinalPrices数量,它们都是10个)?我想也许我可以通过使用产品名来找到最终价格的位置,但是在产品名和FinalPrices之间总是有页面特定的字符。
我该怎么办呢?
这是我试过的:
我看过Python.org的JSON文档,但它对我找到特定的FinalPrice(或者我只是看错了?)
我试着将整个JSON页面变成一个字符串,并使用一些字符串操作(s.find,s[a:b])来获得所需的FinalPrice,但是除了第一个FinalPrice之外,我找不到其他方法来获得任何FinalPrice。
我在google和stack overflow上搜索了这个问题的正确答案。
这就是我到目前为止所做的一切。

最佳答案

您链接的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 function快速了解数据结构:
>>> 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

10-04 22:45