Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading