2017-08-05 48 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'])' –

相关问题