Juan Badal
Juan Badal

Reputation: 71

How to filter a string?

im making a bot that get info from an api, the problem is that it gives me a lot of data and I only need something in particular. I need just to get the balance, miner status and hashrate.

import os
import discord
import requests
from bs4 import BeautifulSoup

token = "DISCORD_TOKEN"
url = "https://eth.crazypool.org/api/accounts/0x54DcB3b38d05940d3A88ADeff5B57f15f7F64A02"
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36'}

client = discord.Client()

@client.event
async def on_ready():
    await client.change_presence(activity=discord.Game(name="Mining eth"))
    print("On!")

@client.event
async def on_message(message):
    page = requests.get(url, headers=headers)
    jk = BeautifulSoup(page.content, "html.parser")
    if message.content == "!miner":
        await message.channel.send(jk)
    if message.content == "!balance":
        await message.channel.send(jk)
    if message.content == "!shares":
        await message.channel.send(jk)


client.run(token)

Output: enter image description here

Upvotes: 3

Views: 96

Answers (3)

Golden Beats
Golden Beats

Reputation: 31

requests.json() will give you the output as a json then you can just treat it like it was the json library.

Upvotes: 2

Pr1orit3 T1ps
Pr1orit3 T1ps

Reputation: 33

Use requests.json() and then just go into the "stats" then "balance"

Upvotes: 1

Quintin
Quintin

Reputation: 125

You can use requests.json() what you would have to do is

page = page.json()
balance = page["stats"]["balance"]

And what this does is it goes into the "stats" section of the json data then into the "balance" which then if you do print(balance) it will print the balance.

Upvotes: 2

Related Questions