Extracting text data from website a website and storing it

Viewed 418
import requests


def get_prices():
    name = ["SeedifyFund"]

    crypto_data = requests.get(
        "https://api.pancakeswap.info/api/tokens".format(",".join(name))).json()["RAW"]

    data = {}
    for i in crypto_data:
        data[i] = {
            "coin": i,
            "priceusd": crypto_data[i]["USD"]["PRICE"],
            "pricebnb": crypto_data[i]["BNB"]["PRICEBNB"],
        }

    return data


if __name__ == "__main__":
    print(get_prices())

Hi all I'm a bit of a noob at coding with python. I started about a year ago and stopped during lockdown. Got back to it now and decided to build a price bot for a crypto currency. I want to extract data from this website: "https://api.pancakeswap.info/api/tokens" and have that data saved in "priceusd" and "pricebnb"

I hope I explained that well enough.

I would like to extract the data from the website however the code that I have used doesn't seem to be working. It give me a "RAW" error each time.

All help will be greatly appreciated. Thanks!

3 Answers
import requests


def get_prices():

    crypto_data = requests.get("https://api.pancakeswap.info/api/tokens")
    response_content = crypto_data.json()['data']

    data = {}
    for i in response_content:
        data[i] = {
            "coin": i,
            "priceusd": response_content[i]["price"],
            "pricebnb": response_content[i]["price_BNB"],
        }

    return data


if __name__ == "__main__":
    print(get_prices())
import telegram
from telegram.ext import Updater
from telegram.ext import CommandHandler
from tracker import get_prices


telegram_bot_token = "API"

updater = Updater(token=telegram_bot_token, use_context=True)
dispatcher = updater.dispatcher


def price(update, context):
    chat_id = update.effective_chat.id
    message = ""

    crypto_data = get_prices()
    for i in crypto_data:
        coin = crypto_data[i]["price_BNB"]
        price = crypto_data[i]["price"]
        message += f"1 SFUND (Currently showing ADA Price) = \n${price:,.2f} USD\n{change_hour:.3f}% 1 Hour Change\n{change_day:.3f}% Daily Change\n\n"

    context.bot.send_message(chat_id=chat_id, text=message)


dispatcher.add_handler(CommandHandler("price", price))
updater.start_polling()

You were just using the wrong keys.

import requests


def get_prices():
    name = ["SeedifyFund"]

    crypto_data = requests.get("https://api.pancakeswap.info/api/tokens").json()["data"]

    data = {}
    for i in crypto_data:
        data[i] = {
            "coin": i,
            "priceusd": crypto_data[i]["price"],
            "pricebnb": crypto_data[i]["price_BNB"],
        }

    return data


if __name__ == "__main__":
    print(get_prices())

Edit:

import requests


def get_prices():
    name = "SeedifyFund"

    crypto_data = requests.get("https://api.pancakeswap.info/api/tokens").json()["data"]

    data = None
    for i in crypto_data:
        current = crypto_data[i]

        if current['name'] == name:
          data = {
              "coin": i,
              "priceusd": current["price"],
              "pricebnb": current["price_BNB"],
          }

    return data


if __name__ == "__main__":
    print(get_prices())
Related