2017-07-13 13 views
0

jsonのコンテンツを含むウェブページには約966の名前がありますが、私のスクリプトでは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": 

答えて

1

あなたのコードに問題はないと思います。 len(data)をチェックすると10が返されます。つまり、結果のリストには10​​個のJSONオブジェクトしか含まれていません。

10を超えると思われる理由がありますか、これらの大きなオブジェクトのそれぞれの内部にあるnameプロパティにアクセスしようとしていますか?

+0

あなたの答えは、Emersonctありがとう。私は長さもチェックした。今は初心者として私にはもっと意味があります。この時点で、セレンについて考えるべきだと思います。 – SIM

+0

@ SMth80:もっと結果を得るには、実際に行う必要はありません。あなたが使用しているURLには、クエリーストリングの引数があります: '&numRows = 10'です。それを大きな数字に変更する(または削除する?)と助けになります。 – Emersonct

+0

それでも十分でない場合は、結果が返されなくなるまで、各呼び出しの 'numRows'量だけ' start'クエリパラメータをインクリメントして、すべての結果をループすることができます。 – Emersonct

1

あなたのJSONオブジェクトの配列なので、ときにループそうでないデータによるitem変数で配列を取得する代わりに、配列のインデックスを取得しています。

あなたはインデックスとしてアイテム変数を使用して配列を取得することができますし、配列への参照を持っていたら、あなたはその後、名前などのプロパティオブジェクトを読み取ることができます。このよう

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>

+0

あなたのお返事ありがとうございますAlexander Higginsそれはうまくいかなかった。私は今このエラーが発生しています: "リストのインデックスは、dictではなく、整数またはスライスでなければなりません" – SIM

関連する問題