【问题标题】:Pulling variables off site that uses the same name从使用相同名称的站点中提取变量
【发布时间】:2017-08-05 17:15:30
【问题描述】:

基本上,我现在尝试仅从 sortedOptions 部分中提取所有 SKUs。有人告诉我这是json,不确定是否正确。现在我只拉第一个。我需要指定什么来拉所有这些? 我最初尝试使用正则表达式并且没有提取任何信息,然后我被告知它是 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
},

【问题讨论】:

  • 为什么要把转换后的对象,转回JSON字符串,然后再转回对象?
  • 它给了我一个错误,表明它需要是一个字符串,如果你有更直接的方法请告诉我。这是我能够从此页面提取信息的唯一方法
  • 您的代码不起作用。
  • @coldspeed 上面的代码输出这个 NIKE Air Vapormax 281460 dropbox.com/s/fxe7q8ksuw5lz9e/…
  • 当我尝试运行您的代码时,r = adapter.send(request, **kwargs) 行会导致 requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661) 错误。

标签: python json regex


【解决方案1】:

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

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

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

【讨论】:

  • 谢谢我使用了你所说的并让它工作了 -- ' for option in data['sortedOptions']: print(option['product']['SKU'])'跨度>
猜你喜欢
  • 1970-01-01
  • 2014-08-16
  • 2019-10-08
  • 1970-01-01
  • 2013-04-16
  • 2020-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多