网页上有大约966个名字,包含json内容但是我的脚本中我只有10个名字。我对json很新,这就是为什么我无法弄清楚我正在犯的错误。我怎样才能得到所有的名字?我正在尝试使用以下代码:
import requests
url = 'https://www.zebra.com/bin/zebra/partnersearch?inMiles=true&start=0&numRows=10&latitude=39.5500507&longitude=-105.7820674&sortOrder=asc&sortBy=distance&country=US&searchRadius=5000'
response = requests.get(url)
data = response.json()
for item in data:
print(item['name'])
该页面的部分json内容为:
[{"id":"001i000001XR9dqAAD","website":"www.resortinternet.com","type":"partner","phoneNumber":"+1.970.262.3515","name":"Resortnet, LLC","logoPresent":"No","logoExtension":"","des":"Technology provider for destination resorts","translatedName":"ResortInternet","dbaName":"ResortInternet","PR":"NA","AN":"6244306","accountType":["Reseller"],"contentType":"parent","countries":["US"],"HSA":[],"countriesAndHsa":["US"],"premierSolutionPartner":false,"premierBusinessPartner":false,"solutionPartner":true,"businessPartner":false,"advancedSpecialistBarcodePrinterSupplies":false,"advancedSpecialistCardPrinters":false,"advancedSpecialistSupplies":false,"advancedSpecialistWirelessNetworks":false,"advancedSpecialistPrintEngines":false,"advancedSpecialistRfid":false,"specialistBarcodePrinterSupplies":false,"specialistCardPrinters":false,"specialistSupplies":false,"specialistWirelessNetworks":false,"specialistPrintEngines":false,"specialistRfid":false,"advancedRepairSpecialistLabelPrinter":false,"advancedRepairSpecialistCardPrinter":false,"advancedRepairSpecialistMobilePrinter":false,"advancedRepairSpecialistPrintEngine":false,"repairSpecialistLabelPrinter":false,"repairSpecialistCardPrinter":false,"repairSpecialistMobilePrinter":false,"repairSpecialistPrintEngine":false,"registeredResellerNoSpecialization":false,"pmiWraps":[{"programName":"Solution Partner","category":"Reseller","id":"001i000001XR9dqAAD_2","type":"pmiWrap","contentType":"child"}],"partnerLocations":[{"locationType":"Headquarters","addressLine1":"117 S 6th Ave.,","addressLine2":"PO Box 2718","city":"Frisco","state":"Colorado","zipCode":"80443","country":"United States","phone":"(970) 262-3515","fax":"(970) 668-9431","latlon":"39.5754576,-106.0952117","distance":16.8,"countryCode":"US","HSA":[],"id":"001i000001XR9dqAAD_0","type":"partnerLocation","contentType":"child"},{"locationType":"Primary Location","addressLine1":"117 S 6th Ave.,","city":"Frisco","state":"Colorado","zipCode":"80443","country":"United States","phone":"+1.970.262.3515","latlon":"39.5754576,-106.0952117","distance":16.8,"countryCode":"US","HSA":[],"id":"001i000001XR9dqAAD_1","type":"partnerLocation","contentType":"child"},{"locationType":"Address","addressLine1":"RESORTINTERNET\r2718:FRISCO:80443\r117 S 6TH AVERM UNIT 2","city":"Frisco","state":"Colorado","stateCode":"CO","zipCode":"80443","country":"United States","latlon":"39.5744309,-106.0975203","distance":16.9,"countryCode":"US","HSA":[],"id":"001i000001XR9dqAAD_100","type":"partnerLocation","contentType":"child"}],"verticalHierarchyWraps":[],"primaryLocation":{"locationType":"Headquarters","addressLine1":"117 S 6th Ave.,","addressLine2":"PO Box 2718","city":"Frisco","state":"Colorado","zipCode":"80443","country":"United States","phone":"(970) 262-3515","fax":"(970) 668-9431","latlon":"39.5754576,-106.0952117","distance":16.8,"countryCode":"US","HSA":
答案 0 :(得分:1)
您的JSON是一个对象数组,因此当您遍历数据时,您不会在item
变量中获取数组,而是获取数组的索引。
您可以使用item变量作为索引来获取数组,一旦引用了数组,就可以读取属性对象,例如name:
像这样:
for index in data:
item = data[index]
print(item['name'])
这是JavaScript:
<script>
var data = [
{
"id":"001i000001XR9dqAAD",
"website":"www.resortinternet.com",
"type":"partner",
"phoneNumber":"+1.970.262.3515",
"name":"Resortnet, LLC",
"logoPresent":"No",
"logoExtension":"",
"des":"Technology provider for destination resorts",
"translatedName":"ResortInternet",
"dbaName":"ResortInternet",
"PR":"NA",
"AN":"6244306",
"accountType":[
"Reseller"
],
"contentType":"parent",
"countries":[
"US"
],
"HSA":[
],
"countriesAndHsa":[
"US"
],
"premierSolutionPartner":false,
"premierBusinessPartner":false,
"solutionPartner":true,
"businessPartner":false,
"advancedSpecialistBarcodePrinterSupplies":false,
"advancedSpecialistCardPrinters":false,
"advancedSpecialistSupplies":false,
"advancedSpecialistWirelessNetworks":false,
"advancedSpecialistPrintEngines":false,
"advancedSpecialistRfid":false,
"specialistBarcodePrinterSupplies":false,
"specialistCardPrinters":false,
"specialistSupplies":false,
"specialistWirelessNetworks":false,
"specialistPrintEngines":false,
"specialistRfid":false,
"advancedRepairSpecialistLabelPrinter":false,
"advancedRepairSpecialistCardPrinter":false,
"advancedRepairSpecialistMobilePrinter":false,
"advancedRepairSpecialistPrintEngine":false,
"repairSpecialistLabelPrinter":false,
"repairSpecialistCardPrinter":false,
"repairSpecialistMobilePrinter":false,
"repairSpecialistPrintEngine":false,
"registeredResellerNoSpecialization":false,
"pmiWraps":[
{
"programName":"Solution Partner",
"category":"Reseller",
"id":"001i000001XR9dqAAD_2",
"type":"pmiWrap",
"contentType":"child"
}
],
"partnerLocations":[
{
"locationType":"Headquarters",
"addressLine1":"117 S 6th Ave.,",
"addressLine2":"PO Box 2718",
"city":"Frisco",
"state":"Colorado",
"zipCode":"80443",
"country":"United States",
"phone":"(970) 262-3515",
"fax":"(970) 668-9431",
"latlon":"39.5754576,-106.0952117",
"distance":16.8,
"countryCode":"US",
"HSA":[
],
"id":"001i000001XR9dqAAD_0",
"type":"partnerLocation",
"contentType":"child"
},
{
"locationType":"Primary Location",
"addressLine1":"117 S 6th Ave.,",
"city":"Frisco",
"state":"Colorado",
"zipCode":"80443",
"country":"United States",
"phone":"+1.970.262.3515",
"latlon":"39.5754576,-106.0952117",
"distance":16.8,
"countryCode":"US",
"HSA":[
],
"id":"001i000001XR9dqAAD_1",
"type":"partnerLocation",
"contentType":"child"
},
{
"locationType":"Address",
"addressLine1":"RESORTINTERNET\r2718:FRISCO:80443\r117 S 6TH AVERM UNIT 2",
"city":"Frisco",
"state":"Colorado",
"stateCode":"CO",
"zipCode":"80443",
"country":"United States",
"latlon":"39.5744309,-106.0975203",
"distance":16.9,
"countryCode":"US",
"HSA":[
],
"id":"001i000001XR9dqAAD_100",
"type":"partnerLocation",
"contentType":"child"
}
],
"verticalHierarchyWraps":[
],
"primaryLocation":{
"locationType":"Headquarters",
"addressLine1":"117 S 6th Ave.,",
"addressLine2":"PO Box 2718",
"city":"Frisco",
"state":"Colorado",
"zipCode":"80443",
"country":"United States",
"phone":"(970) 262-3515",
"fax":"(970) 668-9431",
"latlon":"39.5754576,-106.0952117",
"distance":16.8,
"countryCode":"US"
}
}
];
for (var index in data)
{
var item=data[index];
console.log(item["name"]);
console.log(item);
}
</script>
&#13;
答案 1 :(得分:1)
我认为您的代码存在问题。如果您检查len(data)
,则返回10
,这意味着结果列表仅包含10个(大)JSON对象。
是否有某些原因让您预期超过10个,或者您是否尝试访问每个较大对象内的某些内容的name
属性?