简体   繁体   中英

Accessing JSON API with Python

I got in to the whole ethereum mining thing and I wanted to create a program that reads the JSON API that ethermine pool gives each wallet

import urllib2, json
url = "https://ethermine.org/api/miner_new/c053c7f0b2213462cdb5ddf60bd8cdfc58e3a8af"
response = urllib2.urlopen(url)
data = json.load(urllib2.urlopen(url))
print data

when I try to run it it just shows me this

Traceback (most recent call last):
  File "miner.py", line 3, in <module>
response = urllib2.urlopen(url)
File "C:\Users\jchan\Anaconda2\lib\urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
 File "C:\Users\jchan\Anaconda2\lib\urllib2.py", line 435, in open
response = meth(req, response)
File "C:\Users\jchan\Anaconda2\lib\urllib2.py", line 548, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\jchan\Anaconda2\lib\urllib2.py", line 473, in error
return self._call_chain(*args)
File "C:\Users\jchan\Anaconda2\lib\urllib2.py", line 407, in _call_chain
result = func(*args)
File "C:\Users\jchan\Anaconda2\lib\urllib2.py", line 556, in 
http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden

Use requests , do pip install requests :

import requests
import json

url = "https://ethermine.org/api/miner_new/c053c7f0b2213462cdb5ddf60bd8cdfc58e3a8af"
response = requests.get(url)
if response.status_code != 200:
    print('error {}'.format(response.status_code))
else:
    data = json.loads(response.text)
    print(json.dumps(data, indent=4))

Output:

{
    "reportedHashRate": "0H/s",
    "unpaid": 0,
    "avgHashrate": 0,
    "ethPerMin": 0,
    "usdPerMin": 0,
    "settings": {
        "voteip": "",
        "vote": 0,
        "name": "",
        "email": "",
        "monitor": 0,
        "ip": "",
        "minPayout": 1
    },
    "payouts": [],
    "workers": {},
    "address": "c053c7f0b2213462cdb5ddf60bd8cdfc58e3a8af",
    "hashRate": "0H/s",
    "btcPerMin": 0,
    "rounds": []
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM