1

Hi everyone I’m trying to extracts some contents from this text/JavaScript below:

var item_stock = {"colors": [{"color_code": "1","color_name": "BLACK","color_swatch": "/apis/image/crop?file_name=%2Fphoto%2F2019aw%2F19-04969%2Fs-19-04969_1-1.jpg&width=26&height=26","color_proper": "3,600","color_proper_usd": "33.48","color_proper_eur": "30.24","color_sale": "","color_sale_usd": "","color_sale_eur": "","color_percent": "","html_color": "","image_url": "","stock":  "0","sizes": [{"goods_id": "821","size_code": "OS","size_name": "23-25cm","stock": 0,"size_proper": "3,600","size_proper_usd": "33.48","size_proper_eur": "30.24","size_sale": "","size_sale_usd": "","size_sale_eur": "","size_percent": "","in_sales_flag": "1","stock_request_flag": "","restock_request_flag": 0,"coming_soon_flag": "0",},],"images": "<img src=/photo/2019aw/19-04969/s-19-04969_1-1.jpg>",},{"color_code": "101","color_name": "WHITE","color_swatch": "/apis/image/crop?file_name=%2Fphoto%2F2019aw%2F19-04969%2Fs-19-04969_101-1.jpg&width=26&height=26","color_proper": "3,600","color_proper_usd": "33.48","color_proper_eur": "30.24","color_sale": "","color_sale_usd": "","color_sale_eur": "","color_percent": "","html_color": "","image_url": "","stock":  "3","sizes": [{"goods_id": "822","size_code": "OS","size_name": "23-25cm","stock": 3,"size_proper": "3,600","size_proper_usd": "33.48","size_proper_eur": "30.24","size_sale": "","size_sale_usd": "","size_sale_eur": "","size_percent": "","in_sales_flag": "1","stock_request_flag": "","restock_request_flag": 0,"coming_soon_flag": "0",},],"images": "<img src=/photo/2019aw/19-04969/s-19-04969_101-1.jpg>",},]};

I’m trying to print out “color_name”:”BLACK” and “size_name”:”23-25cm”

This is my code:

js = soup.find_all('script', {'type': 'text/javascript'})[15]
js = json.loads(str(js).split('var item_stock = ')[1]).split('colors: ')[1].split('sizes: ')[1]
for size in js:
    sizee = str(size).split("size_name")[1].split(":")[1]
    print(sizee)
’’’

1
  • 1
    If you want to parse Javascript, it's a good idea to use some form of AST. For Python I've seen this -> github.com/Kronuz/esprima-python Commented Sep 16, 2019 at 10:47

1 Answer 1

1
import json

js = """var item_stock = {"colors": [{"color_code": "1","color_name": "BLACK","color_swatch": "/apis/image/crop?file_name=%2Fphoto%2F2019aw%2F19-04969%2Fs-19-04969_1-1.jpg&width=26&height=26","color_proper": "3,600","color_proper_usd": "33.48","color_proper_eur": "30.24","color_sale": "","color_sale_usd": "","color_sale_eur": "","color_percent": "","html_color": "","image_url": "","stock":  "0","sizes": [{"goods_id": "821","size_code": "OS","size_name": "23-25cm","stock": 0,"size_proper": "3,600","size_proper_usd": "33.48","size_proper_eur": "30.24","size_sale": "","size_sale_usd": "","size_sale_eur": "","size_percent": "","in_sales_flag": "1","stock_request_flag": "","restock_request_flag": 0,"coming_soon_flag": "0",},],"images": "<img src=/photo/2019aw/19-04969/s-19-04969_1-1.jpg>",},{"color_code": "101","color_name": "WHITE","color_swatch": "/apis/image/crop?file_name=%2Fphoto%2F2019aw%2F19-04969%2Fs-19-04969_101-1.jpg&width=26&height=26","color_proper": "3,600","color_proper_usd": "33.48","color_proper_eur": "30.24","color_sale": "","color_sale_usd": "","color_sale_eur": "","color_percent": "","html_color": "","image_url": "","stock":  "3","sizes": [{"goods_id": "822","size_code": "OS","size_name": "23-25cm","stock": 3,"size_proper": "3,600","size_proper_usd": "33.48","size_proper_eur": "30.24","size_sale": "","size_sale_usd": "","size_sale_eur": "","size_percent": "","in_sales_flag": "1","stock_request_flag": "","restock_request_flag": 0,"coming_soon_flag": "0",},],"images": "<img src=/photo/2019aw/19-04969/s-19-04969_101-1.jpg>",},]};"""

# format the string so it is valid json.
js = js.replace('var item_stock = ', '').replace(',}', '}').replace(',]', ']')[:-1]

# load the json
obj = json.loads(js)

# iterate on the colors list
for color in obj['colors']:
    print(f"Color : {color['color_name']}")

    for size in color['sizes']:
        print(f"\t - size : {size['size_name']}")

""" Output :
Color : BLACK
     - size : 23-25cm
Color : WHITE
     - size : 23-25cm
"""
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.