Skip to main content
 首页 » 编程设计

Python从urllib请求中解析json

2025年05月04日8lonelyxmas

我有以下 json,

{ 
findItemsByKeywordsResponse: [ 
{ 
ack: [ 
"Success" 
], 
version: [ 
"1.12.0" 
], 
timestamp: [ 
"2012-08-20T21:42:08.532Z" 
], 
searchResult: [ 
{ 
@count: "2", 
item: [ 
{ 
itemId: [ 
"380459520121" 
], 
title: [ 
"Lenovo ThinkPad T61 Core 2 Duo 2.2GHz 1GB 120GB DVD-RW WiFi Laptop 15" Notebook" 
], 
globalId: [ 
"EBAY-US" 
], 
primaryCategory: [ 
{ 
categoryId: [ 
"177" 
], 
categoryName: [ 
"PC Laptops & Netbooks" 
] 
} 
], 
galleryURL: [ 
"http://thumbs2.ebaystatic.com/pict/3804595201214040_1.jpg" 
], 
viewItemURL: [ 
"http://www.ebay.com/itm/Lenovo-ThinkPad-T61-Core-2-Duo-2-2GHz-1GB-120GB-DVD-RW-WiFi-Laptop-15-Notebook-/380459520121?pt=Laptops_Nov05" 
], 
paymentMethod: [ 
"PayPal", 
"VisaMC", 
"Discover" 
], 
autoPay: [ 
"false" 
], 
postalCode: [ 
"55114" 
], 
location: [ 
"Saint Paul,MN,USA" 
], 
country: [ 
"US" 
], 
shippingInfo: [ 
{ 
shippingType: [ 
"Calculated" 
], 
shipToLocations: [ 
"US" 
], 
expeditedShipping: [ 
"true" 
], 
oneDayShippingAvailable: [ 
"false" 
], 
handlingTime: [ 
"1" 
] 
} 
], 
sellingStatus: [ 
{ 
currentPrice: [ 
{ 
@currencyId: "USD", 
__value__: "112.5" 
} 
], 
convertedCurrentPrice: [ 
{ 
@currencyId: "USD", 
__value__: "112.5" 
} 
], 
bidCount: [ 
"5" 
], 
sellingState: [ 
"Active" 
], 
timeLeft: [ 
"P0DT0H18M18S" 
] 
} 
], 
listingInfo: [ 
{ 
bestOfferEnabled: [ 
"false" 
], 
buyItNowAvailable: [ 
"false" 
], 
startTime: [ 
"2012-08-15T22:00:26.000Z" 
], 
endTime: [ 
"2012-08-20T22:00:26.000Z" 
], 
listingType: [ 
"Auction" 
], 
gift: [ 
"false" 
] 
} 
], 
returnsAccepted: [ 
"true" 
], 
condition: [ 
{ 
conditionId: [ 
"3000" 
], 
conditionDisplayName: [ 
"Used" 
] 
} 
], 
isMultiVariationListing: [ 
"false" 
] 
}, 
{ 
itemId: [ 
"110931951761" 
], 
title: [ 
"Dell Latitude D630 Laptop Core Duo 1.8GHz 2GB Ram 60GB HDD Not Complete" 
], 
globalId: [ 
"EBAY-US" 
], 
primaryCategory: [ 
{ 
categoryId: [ 
"177" 
], 
categoryName: [ 
"PC Laptops & Netbooks" 
] 
} 
], 
galleryURL: [ 
"http://thumbs2.ebaystatic.com/pict/1109319517614040_2.jpg" 
], 
viewItemURL: [ 
"http://www.ebay.com/itm/Dell-Latitude-D630-Laptop-Core-Duo-1-8GHz-2GB-Ram-60GB-HDD-Not-Complete-/110931951761?pt=Laptops_Nov05" 
], 
paymentMethod: [ 
"PayPal" 
], 
autoPay: [ 
"false" 
], 
postalCode: [ 
"17737" 
], 
location: [ 
"Hughesville,PA,USA" 
], 
country: [ 
"US" 
], 
shippingInfo: [ 
{ 
shippingServiceCost: [ 
{ 
@currencyId: "USD", 
__value__: "24.0" 
} 
], 
shippingType: [ 
"Flat" 
], 
shipToLocations: [ 
"US" 
], 
expeditedShipping: [ 
"false" 
], 
oneDayShippingAvailable: [ 
"false" 
], 
handlingTime: [ 
"1" 
] 
} 
], 
sellingStatus: [ 
{ 
currentPrice: [ 
{ 
@currencyId: "USD", 
__value__: "61.99" 
} 
], 
convertedCurrentPrice: [ 
{ 
@currencyId: "USD", 
__value__: "61.99" 
} 
], 
bidCount: [ 
"9" 
], 
sellingState: [ 
"Active" 
], 
timeLeft: [ 
"P0DT0H8M7S" 
] 
} 
], 
listingInfo: [ 
{ 
bestOfferEnabled: [ 
"false" 
], 
buyItNowAvailable: [ 
"false" 
], 
startTime: [ 
"2012-08-17T21:50:15.000Z" 
], 
endTime: [ 
"2012-08-20T21:50:15.000Z" 
], 
listingType: [ 
"Auction" 
], 
gift: [ 
"false" 
] 
} 
], 
returnsAccepted: [ 
"true" 
], 
condition: [ 
{ 
conditionId: [ 
"3000" 
], 
conditionDisplayName: [ 
"Used" 
] 
} 
], 
isMultiVariationListing: [ 
"false" 
] 
} 
] 
} 
], 
paginationOutput: [ 
{ 
pageNumber: [ 
"1" 
], 
entriesPerPage: [ 
"2" 
], 
totalPages: [ 
"486815" 
], 
totalEntries: [ 
"973629" 
] 
} 
], 
itemSearchURL: [ 
"http://www.ebay.com/sch/i.html?_nkw=laptops&_ddo=1&_ipg=2&_pgn=1" 
] 
} 
] 
} 

我尝试使用以下 for 循环获取“项目”数据:

  data = json.load(urllib2.urlopen(url)) 
  #print data 
  for item in data['findItemsByKeywordsResponse'][0]['searchResult'][0]['item']: 
    for itemId in item['itemId']: 
      print itemId 

这工作正常,但我不确定如何实际获取我试图在底部显示的所有项目数据。

[ 
{ 
@count: "2", 
item: [ 
{}, 
{} 
] 
} 
], 

请您参考如下方法:

data['findItemsByKeywordsResponse'] 是一个 list,因此您可以使用 the_list[0] 访问其中的项目,依此类推.

list 中没有键,键属于dict。