使用Python

时间:2018-12-11 16:28:27

标签: python json python-3.x python-requests

我正在尝试解析网站股票的JSON。

JSON:https://www.ssense.com/en-us/men/sneakers.json

因此,我想从用户那里获取一些关键字。然后,我想使用这些关键字解析JSON,以找到项目的名称,并(在这种情况下)返回ID,SKU和URL。

例如: 如果我输入“ Black Fennec”,我想解析JSON并找到Black Fennec运动鞋的ID,SKU和URL(ID为3297299,SKU为191422M237006,URL为/ men / product / ps- paul-smith / black-fennec-sneakers / 3297299)

我从来没有尝试做过这样的事情。基于一些显示如何解析JSON的指南,我从这里开始:

r = requests.Session()
stock = r.get("https://www.ssense.com/en-us/men/sneakers.json",headers = headers)
obj json_data = json.loads(stock.text)

但是我现在很困惑。如何根据关键字查找产品,如何获取ID,网址和SKU或它?

1 个答案:

答案 0 :(得分:4)

有多种方法可以处理输出。不知道要怎么做。但这应该可以帮助您。

编辑1:

import requests

r = requests.Session()
obj_json_data = r.get("https://www.ssense.com/en-us/men/sneakers.json").json()

products = obj_json_data['products']


keyword = input('Enter a keyword: ')

for product in products:
    if keyword.upper() in product['name'].upper():
        name = product['name']
        id_var = product['id']
        sku = product['sku']
        url = product['url']
        print ('Product: %s\nID: %s\nSKU: %s\nURL: %s' %(name, id_var, sku, url))
        # if you only want to return the first match, uncomment next line
        #break

我也将其设置为将其存储到数据帧或列表中。只是给出一些选择的选择。

import requests
import pandas as pd

r = requests.Session()
obj_json_data = r.get("https://www.ssense.com/en-us/men/sneakers.json").json()

products = obj_json_data['products']



keyword = input('Enter a keyword: ')

products_found = []
results = pd.DataFrame()
for product in products:
    if keyword.upper() in product['name'].upper():
        name = product['name']
        id_var = product['id']
        sku = product['sku']
        url = product['url']
        temp_df = pd.DataFrame([[name, id_var, sku, url]], columns=['name','id','sku','url'])
        results = results.append(temp_df)
        products_found = products_found.append(name)
        print ('Product: %s\nID: %s\nSKU: %s\nURL: %s' %(name, id_var, sku, url))

if products_found == []:
    print ('Nothing found')

编辑2:这是通过将json转换为数据框,然后按名称中包含关键字的行进行过滤的另一种方法(在我看来,这实际上是一种更好的解决方案)

import requests
import pandas as pd
from pandas.io.json import json_normalize

r = requests.Session()
obj_json_data = r.get("https://www.ssense.com/en-us/men/sneakers.json").json()

products = obj_json_data['products']
products_df = json_normalize(products)

keyword = input('Enter a keyword: ')

products_found = []
results = pd.DataFrame()

results = products_df[products_df['name'].str.contains(keyword, case = False)]
#print (results[['name', 'id', 'sku', 'url']])

products_found = list(results['name'])
if products_found == []:
    print ('Nothing found')
else:
    print ('Found: '+ str(products_found))