从使用相同名称的站点拉出变量

时间:2017-08-05 17:15:31

标签: python json regex

基本上我现在只想从SKU部分提取所有sortedOptions s。有人告诉我这是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 个答案:

答案 0 :(得分:1)

您提供的JSON无效。无论如何,我认为“sortedOptions”是一个列表。所以这应该让你感到高兴:

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

如需更完整的答案,请提供有效的json。提供的一个是。提示:使用curlwget