How to keep only a certain part of a string, and a certain amount of characters after it

Question:

I’m trying to make a script that will get the current sell price of an item in a game, and I want it to end up printing something along the lines of:

"sellPrice":1451.1

However, the number (shown as 1451.1 in the example) frequently changes, and I am not able to only get this number alone as an output because there are many other numbers in the string, and they are also frequently changing. I tried to replace everything but the part of the string that I want, but since there are so many numbers that are frequently changing, I was unable to do so. I was thinking it might be possible to find the word "sellPrice" in the string, since it never changes, and then just print a certain amount of characters after it. However, I can’t find out how to do this and was wondering if there are any other solutions.

The website I am getting the data from is this, and the large block of text on the page is the string that I’m trying to use to find the sell price.

My current code (prints entire HTML of the website, with no sorting):

import requests

URL = "https://sky.coflnet.com/api/bazaar/ENCHANTED_COAL/snapshot"
page = requests.get(URL)

print(page.text)

I am pretty new to python / coding in general, so I’m sorry in advance if there is some blatantly obvious solution to this that I just don’t know yet. Thanks!

Asked By: fwaf fwaf

||

Answers:

As per Pranav – use JSON:

import requests

URL = "https://sky.coflnet.com/api/bazaar/ENCHANTED_COAL/snapshot"
page = requests.get(URL)
data = page.json()
sellPrice = data['sellPrice']

print(f'Sell Price: {sellPrice}')

OUTPUT:

Sell Price: 1378.5
Answered By: ScottC
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.