从使用相同nam的站点中提取变量

2024-09-29 04:26:26 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我现在只想从sortedOptions部分提取所有的SKU。我被告知是json,不确定这是否正确。现在我只拉第一个。我需要指定什么来提取所有这些文件? 我最初尝试使用regex,并没有提取任何信息,然后我被告知这是json,我需要使用json。到目前为止,这是唯一的方法,我已经使用能够从网站上拉任何东西。你知道吗

然后我的下一步是只在同一节中拉stock statusname。你知道吗

代码:

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
},

Tags: namejsonfalsetrueproductamountnullprice
1条回答
网友
1楼 · 发布于 2024-09-29 04:26:26

您提供的JSON无效。不管怎样,我假设“sortedOptions”是一个列表。所以这应该会启发你:

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

要获得更完整的答案,请提供有效的json。提供的一个tou是而不是。提示:使用curlwget。你知道吗

相关问题 更多 >