Home > Software design >  Is there any way to get a spesific data from api link?
Is there any way to get a spesific data from api link?

Time:05-17

I'm trying to print a spesific and cheapest price of item that selected from api link. But the issue is when a seller lists new item the api link changes too. For example here is my script and price api link: item link and api link

import requests, json

params = {
    'game': 'csgo',
    'item': 'desert-eagle-light-rail-field-tested'
}

response = requests.get('https://integration.bynogame.com/api/listing/list/', params=params)

ads = json.loads(response.text)["response"]["data"]["ads"][5]
print("Fiyat:", ads["price"], "Stok:", ads["count"], "Satıcı Adı:", ads["sellerMarketName"], sep="  ")
ads = json.loads(response.text)["response"]["data"]["ads"][4]
print("Fiyat:", ads["price"], "Stok:", ads["count"], "Satıcı Adı:", ads["sellerMarketName"], sep="  ")
ads = json.loads(response.text)["response"]["data"]["ads"][3]
print("Fiyat:", ads["price"], "Stok:", ads["count"], "Satıcı Adı:", ads["sellerMarketName"], sep="  ")
ads = json.loads(response.text)["response"]["data"]["ads"][2]
print("Fiyat:", ads["price"], "Stok:", ads["count"], "Satıcı Adı:", ads["sellerMarketName"], sep="  ")
ads = json.loads(response.text)["response"]["data"]["ads"][1]
print("Fiyat:", ads["price"], "Stok:", ads["count"], "Satıcı Adı:", ads["sellerMarketName"], sep="  ")
ads = json.loads(response.text)["response"]["data"]["ads"][0]
print("Fiyat:", ads["price"], "Stok:", ads["count"], "Satıcı Adı:", ads["sellerMarketName"], sep="  ")

This is first script i made but i cant figured it out how to print lowest price. Mine prints all listings if response > data > ads number if it matches. If not the output looks like this: image link

If i figure out how to print only lowest price, im gonna loop it.

CodePudding user response:

You could do something like this:

ads = json.loads(response.text)["response"]["data"]['ads'] # grab all ads
lowest_price = ads[0]['price'] # use the first ad as the max price
for item in ads[1:]: # go through all ad items (besides first item)
    if item['price'] < lowest_price: # find the lowest price by comparing
        lowest_price = item['price'] # update lowest price if it beats previous low
print(lowest_price) # print out the final lowest price
  • Related