Issue
I made a request to Pancake swap API, I only need the first 6 digits of the price in Bold letter below. The final goal is to get a notification when price goes below or above X price.
this what I get from the API:
{"updated_at":1644088600598,"data":{"name":"SugarBounce","symbol":"TIP","price":"0.389597450332099015858871009432","price_BNB":"0.0009419388066193731353156275432376"}}
I'm using BeautifulSoup and request library.
url = 'https://api.pancakeswap.info/api/v2/tokens/0x40f906e19b14100d5247686e08053c4873c66192'
data = requests.get(url)
soup = BeautifulSoup(data.content, 'html.parser')
Solution
You can do the following to get the first 6 digits of the price
The price is sent as a string therefore you can do some list slicing to get the digits that you would like.
import requests
import json
url = 'https://api.pancakeswap.info/api/v2/tokens/0x40f906e19b14100d5247686e08053c4873c66192'
data = requests.get(url).json()
price = float(data['data']['price'][:6]) # to get the first 6 digits of the price
Though the price is a float and would probably just be better off comparing the previous price to this new price from the API call.
Answered By - Andrew Ryan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.