0

I am having some trouble understanding json dictionary and array. I have a script that is scraping information from a website.

models.txt is just a list of model numbers such as

30373
30374
30375

and json_descriptions.txt is a list of the keys I want

sku
price
listprice
issoldout

The code is:

import urllib
import re
import json

modelslist = open("models.txt").read()
modelslist = modelslist.split("\n")
descriptionlist = open("json_descriptions.txt").read()
descriptionlist = descriptionlist.split("\n")

for model in modelslist:
    htmltext = urllib.urlopen("http://dx.com/p/GetProductInfoRealTime?skus="+model)
    htmltext = json.load(htmltext)
    if htmltext['success'] == True:
        def get_data(dict_index, key):
            return htmltext[u"data"][dict_index][key]
        for description in descriptionlist:
           info = description, (get_data(0,description))
           print info
    else:
       print "product does not exist"

If I print out info I get:

sku 30373
price 9.10
listprice 17.62
issoldout False

so that means info[0] is:

sku
price
listprice
issoldout

and info[1] is:

30373
9.10
17.62
False

I would like to know if there is a way that I can have this: loop 1 = ['sku','30373','price','4.90','listprice','0','issoldout','False'] loop 2 = ['sku','30374','price','10.50','listprice','0','issoldout','False']

info[0] = sku info[1] = 30373 info[2] = price info[3] = 4.90 info[4] = listprice info[5] = 0 info[6] = issoldout info[7] = False and then repeat that with a new list for the next loop through.

I have tried using info = json.dumps(info) but that just gives info[0] = [[[[ and info[1] = """" info[2] = spli and so on

2
  • Are you sure this is an array and not a dictionary? The way you're using it suggests the latter. Commented Apr 9, 2013 at 2:58
  • yeah, my fault... still learning, I'll edit it. Commented Apr 9, 2013 at 3:03

1 Answer 1

1

Like this?

info = []
for model in modelslist:
    htmltext = urllib.urlopen("http://dx.com/p/GetProductInfoRealTime?skus="+model)
    htmltext = json.load(htmltext)
    if htmltext['success'] == True:
        def get_data(dict_index, key):
            return htmltext[u"data"][dict_index][key]
        for description in descriptionlist:
            info.append(description)
            info.append(get_data(0,description))
print info
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.