2017-08-05 56 views
-1

基本上我現在試圖從sortedOptions部分只拉動所有SKU。我被告知這是json,不確定這是否正確。現在我只是拉第一個。我需要指定什麼來吸引所有人? 我最初嘗試使用正則表達式,並沒有拉任何信息,然後我被告知它是json,我需要使用json。到目前爲止,這是我以前唯一能夠從網站上獲取任何東西的方法。將變量從站點拉出來,使用相同的名稱

那麼我的下一步就是將stock statusname只在同一部分。

CODE:從網站輸出

import requests 
import json 
r = requests.get('https://randomwebsite.com/') 
pull = r.json() 
print " " 

string = json.dumps(pull) 

parsed_json = json.loads(string) 
print parsed_json['name'] 
print parsed_json['SKU'] 

{ 
"ID": "281460", 
"SKU": "281460", 
"isVisible": true, 
"isOption": false, 
"parentSKU": null, 
"trackingSKU": "281460", 
"name": "NIKE Air Vapormax", 
"model": null, 
"brand": { 
"id": 
"ID": "9D63FD8C31DBD53AB2A9D51F4D9DBBCA", 
"name": "NIKE", 
"clientID": null, 
"sortOrder": null, 
"image": null, 
"target": "", 
"filters": { 
"brandname": "nike" 
} 
}, 
"sortedOptions": [ 
{ 
"name": "5.5", 
"product": { 
"SKU": "281460.463725", 
"isDefault": false, 
"price": { 
"amount": "180.00", 
"currency": "GBP" 
}, 
"previousPrice": null, 
"RRP": { 
"amount": "180.00", 
"currency": "GBP" 
}, 
"costPrice": null, 
"taxCode": null, 
"taxRate": null, 
"stockStatus": "IN STOCK", 
"colour": "", 
"colourDescription": "GRY/GRY-SPECK", 
"size": null, 
"exactSize": null, 
"sortOrder": "1", 
"optionTypes": [ 
"UK Size" 
] 
}, 
"isLeaf": true 
}, 
{ 
"name": "6", 
"product": { 
"SKU": "281460.595347", 
"isDefault": false, 
"price": { 
"amount": "180.00", 
"currency": "GBP" 
}, 
"previousPrice": null, 
"RRP": { 
"amount": "180.00", 
"currency": "GBP" 
}, 
"costPrice": null, 
"taxCode": null, 
"taxRate": null, 
"stockStatus": "IN STOCK", 
"colour": "", 
"colourDescription": "GRY/GRY-SPECK", 
"size": null, 
"exactSize": null, 
"sortOrder": "2", 
"optionTypes": [ 
"UK Size" 
] 
}, 
"isLeaf": true 
}, 
{ 
"name": "6.5", 
"product": { 
"SKU": "281460.463895", 
"isDefault": false, 
"price": { 
"amount": "180.00", 
"currency": "GBP" 
}, 
"previousPrice": null, 
"RRP": { 
"amount": "180.00", 
"currency": "GBP" 
}, 
"costPrice": null, 
"taxCode": null, 
"taxRate": null, 
"stockStatus": "IN STOCK", 
"colour": "", 
"colourDescription": "GRY/GRY-SPECK", 
"size": null, 
"exactSize": null, 
"sortOrder": "3", 
"optionTypes": [ 
"UK Size" 
] 
}, 
"isLeaf": true 
}, 
{ 
"name": "7.5", 
"product": { 
"SKU": "281460.595350", 
"isDefault": false, 
"price": { 
"amount": "180.00", 
"currency": "GBP" 
}, 
"previousPrice": null, 
"RRP": { 
"amount": "180.00", 
"currency": "GBP" 
}, 
"costPrice": null, 
"taxCode": null, 
"taxRate": null, 
"stockStatus": "IN STOCK", 
"colour": "", 
"colourDescription": "GRY/GRY-SPECK", 
"size": null, 
"exactSize": null, 
"sortOrder": "5", 
"optionTypes": [ 
"UK Size" 
] 
}, 
"isLeaf": true 
}, 
{ 
"name": "8", 
"product": { 
"SKU": "281460.595352", 
"isDefault": false, 
"price": { 
"amount": "180.00", 
"currency": "GBP" 
}, 
"previousPrice": null, 
"RRP": { 
"amount": "180.00", 
"currency": "GBP" 
}, 
"costPrice": null, 
"taxCode": null, 
"taxRate": null, 
"stockStatus": "IN STOCK", 
"colour": "", 
"colourDescription": "GRY/GRY-SPECK", 
"size": null, 
"exactSize": null, 
"sortOrder": "6", 
"optionTypes": [ 
"UK Size" 
] 
}, 
"isLeaf": true 
}, 
{ 
"name": "9", 
"product": { 
"SKU": "281460.595359", 
"isDefault": false, 
"price": { 
"amount": "180.00", 
"currency": "GBP" 
}, 
"previousPrice": null, 
"RRP": { 
"amount": "180.00", 
"currency": "GBP" 
}, 
"costPrice": null, 
"taxCode": null, 
"taxRate": null, 
"stockStatus": "IN STOCK", 
"colour": "", 
"colourDescription": "GRY/GRY-SPECK", 
"size": null, 
"exactSize": null, 
"sortOrder": "8", 
"optionTypes": [ 
"UK Size" 
] 
}, 
"isLeaf": true 
}, 
+1

爲什麼你要轉換的對象,把它們變回JSON字符串,然後再回到對象? – jonrsharpe

+0

它給了我一個錯誤,指出它需要是一個字符串,如果你有一個更直接的方法,請讓我知道。這是我能夠從此頁面獲取信息的唯一途徑 –

+0

您的代碼無法使用。 –

回答

1

您提供的JSON是無效的。無論如何,我假設「sortedOptions」是一個列表。所以,這應該讓你明白:

for option in parsed_json['sortedOptions']: 
    print option.keys() 

如需更完整的答案,請提供有效的json。提供的一個tou是而不是。提示:使用curlwget

+0

謝謝我使用這個你說什麼,並得到它的工作 - '在數據選項['sortedOptions']: print(option ['product'] ['SKU'])' –

相關問題