2016-04-01 14 views
0

Sony'nin PlayStation mağazasından bilgi taranan bir Web uygulaması yazmaya çalışıyorum. İstediğim verilere sahip olan JSON dosyasını buldum, ancak JSON dosyasının yalnızca belirli öğelerini saklamak için Scrapy'yi nasıl kullanacağımı merak ediyorum.İç içe JSON verilerini kazıyarak Scrapy'yi mi kullanıyorsunuz?

{ 
    "age_limit":0, 
    "attributes":{ 
     "facets":{ 
      "platform":[ 
       {"name":"PS4™","count":96,"key":"ps4"}, 
       {"name":"PS3™","count":5,"key":"ps3"}, 
       {"name":"PS Vita","count":7,"key":"vita"}, 
      ] 
     } 
    } 
    } 

Ben sadece "isim" PS4 için "sayılan" değerini istiyorum:

İşte JSON veri parçası. Bunu Scrapy'de nasıl alabilirim? İşte benim Scrapy kodum şu ana kadar:

from scrapy.spider import BaseSpider 
from scrapy.selector import HtmlXPathSelector 
from crossbuy.items import PS4Vita 


class PS4VitaSpider(BaseSpider): 
    name = "ps4vita" # Name of the spider, to be used when crawling 
    allowed_domains = ["store.playstation.com"] # Where the spider is allowed to  go 
    start_url = "https://store.playstation.com/chihiro-api/viewfinder/US/en/999/STORE-MSF77008-9_PS4PSVCBBUNDLE?size=30&gkb=1&geoCountry=US" 

    def parse(self, response): 
     jsonresponse = json.loads(response) 

     pass # To be changed later 

Teşekkürler!

+0

sadece { "adı": "PS4} erişemiyor musunuz?. Normal şekilde örneğin' [p [ p ["isim"] == "PS4 ™"] 'ise, jsonresponse [" öznitelikler "] [" yüzler "] [" platform "] p için" saymak "]? – Anzel

cevap

1
... 
def parse(self, response): 
    jsonresponse = json.loads(response.body) 
    my_count = None 
    for platform in jsonresponse['attributes']['facets']['platform']: 
     if 'PS4' in platform['name']: 
      my_count = platform['count'] 

    yield dict(count=my_count) 
... 
0

Basitçe senin kadar json verilere erişmek olur bir piton sözlüğü:

# To get a list of the counts: 
counts = [x['count'] for x in jsonresponse['attributes']['facets']['platform']]