Hack Forums
[Python] URLLib read between characters - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] URLLib read between characters (/showthread.php?tid=4830908)



URLLib read between characters - SIM-2552982 - May 21, 2015

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


* - Black‌jack - May 21, 2015

Uhh, how about using a JSON parser like this API was meant to be used?
Have a look at the Python `json` module.


* - -zYMTOM' - May 22, 2015

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.


* - Black‌jack - May 22, 2015

(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()`


* - SIM-2552982 - May 22, 2015

Thanks guys, helped me out so much! This was what I was looking for. Thanks again


* - Tony S - May 22, 2015

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