Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

python - Any help, stuck with my code when parsing a JSON from HTML. Needs to tell me when stock is True

I have this JSON output in a HTML and I want to check the stock. I build everything already but I am stuck at the part when Python needs to tell me if the stock is true or not.

All the numbers are stores around the Netherlands. I just want to code that Python prints ''In Stock'' if only ONE of them is TRUE. I did the '' if ... or ... == 'True', but then if one of the stores is False, it's telling me it's still out of stock.

Any idea what kind of code I need to use to let Python tell me if one of the stores has stock?

I am using BS4, Beautifulsoup to parse the JSON.

Just stuck at the ''If... == 'True' part.

Thanks!

{"1665134":{"642":{"hasStock":false,"hasShowModel":false,"lowStock":false},"1298":{"hasStock":false,"hasShowModel":false,"lowStock":false},"1299":{"hasStock":false,"hasShowModel":false,"lowStock":false},"1322":{"hasStock":false,"hasShowModel":false,"lowStock":false},"1325":{"hasStock":false,"hasShowModel":false,"lowStock":false},"1966":{"hasStock":false,"hasShowModel":false,"lowStock":false},"1208":{"hasStock":false,"hasShowModel":false,"lowStock":false},"193":{"hasStock":false,"hasShowModel":false,"lowStock":false},"194":{"hasStock":false,"hasShowModel":false,"lowStock":false},"1102":{"hasStock":false,"hasShowModel":false,"lowStock":false},"1360":{"hasStock":false,"hasShowModel":false,"lowStock":false},"852":{"hasStock":false,"hasShowModel":false,"lowStock":false},"853":{"hasStock":false,"hasShowModel":false,"lowStock":false},"854":{"hasStock":false,"hasShowModel":false,"lowStock":false},"1239":{"hasStock":false,"hasShowModel":false,"lowStock":false},"855":{"hasStock":false,"hasShowModel":false,"lowStock":false},"856":{"hasStock":false,"hasShowModel":false,"lowStock":false},"857":{"hasStock":false,"hasShowModel":false,"lowStock":false},"858":{"hasStock":false,"hasShowModel":false,"lowStock":false},"859":{"hasStock":false,"hasShowModel":false,"lowStock":false},"860":{"hasStock":false,"hasShowModel":false,"lowStock":false},"861":{"hasStock":false,"hasShowModel":false,"lowStock":false},"1246":{"hasStock":false,"hasShowModel":false,"lowStock":false},"862":{"hasStock":false,"hasShowModel":false,"lowStock":false},"863":{"hasStock":false,"hasShowModel":false,"lowStock":false},"864":{"hasStock":false,"hasShowModel":false,"lowStock":false},"865":{"hasStock":false,"hasShowModel":false,"lowStock":false},"866":{"hasStock":false,"hasShowModel":false,"lowStock":false},"867":{"hasStock":false,"hasShowModel":false,"lowStock":false},"484":{"hasStock":false,"hasShowModel":false,"lowStock":false},"1380":{"hasStock":false,"hasShowModel":false,"lowStock":false},"868":{"hasStock":false,"hasShowModel":false,"lowStock":false},"869":{"hasStock":false,"hasShowModel":false,"lowStock":false},"1381":{"hasStock":false,"hasShowModel":false,"lowStock":false},"870":{"hasStock":false,"hasShowModel":false,"lowStock":false},"1255":{"hasStock":false,"hasShowModel":false,"lowStock":false},"871":{"hasStock":false,"hasShowModel":false,"lowStock":false},"360":{"hasStock":false,"hasShowModel":false,"lowStock":false},"872":{"hasStock":false,"hasShowModel":false,"lowStock":false},"873":{"hasStock":false,"hasShowModel":false,"lowStock":false},"746":{"hasStock":false,"hasShowModel":false,"lowStock":false},"875":{"hasStock":false,"hasShowModel":false,"lowStock":false},"876":{"hasStock":false,"hasShowModel":false,"lowStock":false},"749":{"hasStock":false,"hasShowModel":false,"lowStock":false},"1391":{"hasStock":false,"hasShowModel":false,"lowStock":false},"880":{"hasStock":false,"hasShowModel":false,"lowStock":false},"499":{"hasStock":false,"hasShowModel":false,"lowStock":false},"1275":{"hasStock":false,"hasShowModel":false,"lowStock":false},"1149":{"hasStock":false,"hasShowModel":false,"lowStock":false},"637":{"hasStock":false,"hasShowModel":false,"lowStock":false}}}

Python code;

def monitor():
    try:
        response = requests.get(url, headers=headers)
        soup = BeautifulSoup(response.content, 'html.parser')
        voorraad = response.json()
        v1 = (voorraad['{}'.format(productid)]['193']['hasStock'])
        v2 = (voorraad['{}'.format(productid)]['194']['hasStock'])
        v3 = (voorraad['{}'.format(productid)]['360']['hasStock'])
        v4 = (voorraad['{}'.format(productid)]['484']['hasStock'])
        v5 = (voorraad['{}'.format(productid)]['499']['hasStock'])
        v6 = (voorraad['{}'.format(productid)]['637']['hasStock'])
        v7 = (voorraad['{}'.format(productid)]['642']['hasStock'])
        v8 = (voorraad['{}'.format(productid)]['746']['hasStock'])
        v9 = (voorraad['{}'.format(productid)]['749']['hasStock'])
        v10 = (voorraad['{}'.format(productid)]['852']['hasStock'])
        v11 = (voorraad['{}'.format(productid)]['853']['hasStock'])
        v12 = (voorraad['{}'.format(productid)]['854']['hasStock'])
        v13 = (voorraad['{}'.format(productid)]['855']['hasStock'])
        v14 = (voorraad['{}'.format(productid)]['856']['hasStock'])
        v15 = (voorraad['{}'.format(productid)]['857']['hasStock'])
        v16 = (voorraad['{}'.format(productid)]['858']['hasStock'])
        v17 = (voorraad['{}'.format(productid)]['859']['hasStock'])
        v18 = (voorraad['{}'.format(productid)]['860']['hasStock'])
        v19 = (voorraad['{}'.format(productid)]['861']['hasStock'])
        v20 = (voorraad['{}'.format(productid)]['862']['hasStock'])
        v21 = (voorraad['{}'.format(productid)]['863']['hasStock'])
        v22 = (voorraad['{}'.format(productid)]['864']['hasStock'])
        v23 = (voorraad['{}'.format(productid)]['865']['hasStock'])
        v24 = (voorraad['{}'.format(productid)]['866']['hasStock'])
        v25 = (voorraad['{}'.format(productid)]['867']['hasStock'])
        v26 = (voorraad['{}'.format(productid)]['868']['hasStock'])
        v27 = (voorraad['{}'.format(productid)]['869']['hasStock'])
        v28 = (voorraad['{}'.format(productid)]['870']['hasStock'])
        v29 = (voorraad['{}'.format(productid)]['871']['hasStock'])
        v30 = (voorraad['{}'.format(productid)]['872']['hasStock'])
        v31 = (voorraad['{}'.format(productid)]['873']['hasStock'])
        v32 = (voorraad['{}'.format(productid)]['875']['hasStock'])
        v33 = (voorraad['{}'.format(productid)]['876']['hasStock'])
        v34 = (voorraad['{}'.format(productid)]['880']['hasStock'])
        v35 = (voorraad['{}'.format(productid)]['1102']['hasStock'])
        v36 = (voorraad['{}'.format(productid)]['1149']['hasStock'])
        v37 = (voorraad['{}'.format(productid)]['1208']['hasStock'])
        v38 = (voorraad['{}'.format(productid)]['1239']['hasStock'])
        v39 = (voorraad['{}'.format(productid)]['1246']['hasStock'])
        v40 = (voorraad['{}'.format(productid)]['1255']['hasStock'])
        v41 = (voorraad['{}'.format(productid)]['1275']['hasStock'])
        v42 = (voorraad['{}'.format(productid)]['1298']['hasStock'])
        v43 = (voorraad['{}'.format(productid)]['1299']['hasStock'])
        v44 = (voorraad['{}'.format(productid)]['1322']['hasStock'])
        v45 = (voorraad['{}'.format(productid)]['1325']['hasStock'])
        v46 = (voorraad['{}'.format(productid)]['1360']['hasStock'])
        v47 = (voorraad['{}'.format(productid)]['1380']['hasStock'])
        v48 = (voorraad['{}'.format(productid)]['1381']['hasStock'])
        v49 = (voorraad['{}'.format(productid)]['1391']['hasStock'])
        v50 = (voorraad['{}'.format(productid)]['1966']['hasStock'])

        if any(v1, v2, v3):
            print(colored('[{}] ' + 'IN STOCK | ' + (product_title), 'green').format(str(datetime.now())))
            send_to_discord(product_title, webpagina, footerlogo, url, image_url)
            time.sleep(50)
            exit()
        else:
            print(colored('[{}] ' + 'OUT OF STOCK | ' + (product_title), 'red').format(str(datetime.now())))
            time.sleep(2)

Any was a test, not familiar with it...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code will get way out of hand if you have that many manual entries that each do the same thing. First, I'd suggest making a list of product codes

products = [193, 194, 360, 384, ...]

the response.json() from js isn't what you'd use in python. first import

import json

then use json.loads() or json.dumps() to 'parse' and 'stringify' respectively

store = json.loads(<soup.whatever>)

and then I assume "1665134" is a merch id or something, which you can just iterate through subsequent objects

for product in store:
    if(product['hasStock']):
        # do stuff with stock
    else:
        # has no stock, you're sol

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

1.4m articles

1.4m replys

5 comments

57.0k users

...