Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

URLLib read between characters

So I was just experimenting with API's and Python. I made a little program where you fill in a currency and a value, it grabs the information from the API and then gives you the amount of BTC you could buy. Blockchain had a perfect API for this purpose. Now I am trying something different with OMC. I am using this API: https://omnicha.in/api/?method=getinfo

I want the program to read the OMC value, so I wrote this:
(This is just a little part of the whole code)
Code
try:
      response = urlopen(request)
      omc_usd1 = response.read()
      omc_usd1 = omc_usd1[247:253]
      print ('OMC USD Price') +(omc_usd1)+(' ')
      time.sleep(59)
The problem is, that when you take a look at the API, there are a lot of other values on that page. So when they change, and my script still prints everything between characters 247 and 253, things will change their position and mess up the response.

Example: https://gyazo.com/c048e3a5facdb79799900e00dc7c3bae
or: https://gyazo.com/b26cec8e2bca4ec6a17f38c78a13e071

My question, could I use the URLLib module to print the text that's between certain characters? For example; saying it should print everything that's between omc_usd_price": and ,"market_cap". I don't know if there is some sort of method to do this, or to achieve something similar. Could you guys help me out? I am not that good at python yet
Uhh, how about using a JSON parser like this API was meant to be used?
Have a look at the Python `json` module.
I love this user <3
Spoiler (Click to View)
Never rely on the position of the data, as that might change. Always try to actually parse it.

Decoding the json into an array is what you should use
e.g
Code
response = urlopen(request)
omc_usd1 = response.read()
omc_usd1 = json.load(omc_usd1)
print ('OMC USD Price') +(omc_usd1['response']['omc_usd_price'])+(' ')
Here it prints the USD price.
(May 22, 2015 12:06 AM)-zYMTOM' Wrote:  Never rely on the position of the data, as that might change. Always try to actually parse it.

Decoding the json into an array is what you should use
e.g
Code
response = urlopen(request)
omc_usd1 = response.read()
omc_usd1 = json.load(omc_usd1)
print ('OMC USD Price') +(omc_usd1['response']['omc_usd_price'])+(' ')
Here it prints the USD price.

You need to use `json.loads()` if you're passing in a `str`. Either use `json.loads()` or remove `.read()` on the response and pass that to `json.load()`
I love this user <3
Spoiler (Click to View)
Thanks guys, helped me out so much! This was what I was looking for. Thanks again
An even better solution would be to ditch urllib and migrate your code to work with Requests. It makes working with different protocols so much easier. It even has a built in json() method that you can use on request objects - meaning less imports which is always nice.

>>> import requests
>>> requests.get('http://omnicha.in/api/?method=getinfo').json()['response']['omc_usd_price']
0.0028


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by N0TR3DAMUSMay 17, 2019 07:58 PM 8 139 May 17, 2019 07:58 PM
Last Post: demarco
[Python] by PeenutttJul 30, 2017 05:36 PM 8 207 Jul 30, 2017 05:36 PM
Last Post: Peenuttt
[Python] by 41phafr34kJul 9, 2017 12:25 PM 8 146 Jul 9, 2017 12:25 PM
Last Post: nizarnizario