Python 3 Cannot View Hash Rate from Mining Pool in API because Module Cannot Load

Asked 2 years ago, Updated 2 years ago, 129 views

The trouble is as stated in the title.

Here's what I want to do.
*Additional note: I was pointed out that the print statement is of the 2
I don't know how to correct it.Thank you for your cooperation

Python retrieves hash rates from mining pools from the API and displays them from JSON

#python3

-*- coding —utf-8-*-
Modules to Import* -

import urllib.request
import urllib.parse
import json
import sys
import codecs


# python3
Get JSON format string results from webAPI
def dataGet():

     URI scheme
    url='

     Data for URI Parameters 
    param = {
        'address': 'worker', #ID of the person you want to retrieve
        'type': 'json' #specifying data to retrieve
    }

     Get JSON from webAPI
    response=readObj.read()

     print type(response)#>><type 'str'>
 
# Creating a String for URI Parameters
    shaped with paramStr=urllib.request(param)#type=json

    return response

Convert data from webAPI to JSON
def jsonConversion(jsonStr):

     Convert JSON data obtained from webAPI to python data
    data=json.loads(jsonStr)
    return data

     Convert to Unicode because Japanese will be like u'\u767d'
     return json.dumps (data[0], sense_ascii=False)


if__name__=='__main__':

    resStr=dataGet()
    res=jsonConversion(resStr)

    display acquired data
    for item in res:
        print(item.dataGet())

After executing the above code,

#python3
 File "zec3.py", line 43, in <module>
    resStr=dataGet()
  File "zec3.py", line 25, indataGet
    paramStr=urllib.request(param) 
  shaped with type = json
TypeError: 'module' object is not callable

I understand that the module does not load with the error
I don't know how to deal with it.Thank you for your cooperation.

python3 json api

2022-09-30 21:37

2 Answers

urllib.request is a module and cannot be used like urllib.request(param). You must run the request by combining urllib.request.request and urllib.request.urlopen.

It is recommended that you refer to the documentation or recreate it using the relatively easy requests library.
21.6.urlib.request —Expandable library for opening URLs—Python 3.6.5 document
Requests:HTTP for Humans™—Requests 2.20.1 documentation


2022-09-30 21:37

If you rewrite it, you will be able to get the API with the following code.urlib is sufficient if WebAPI knows it is in JSON format.

import urlib.request
import urllib.parse
import json

def dataGet():
    # url was blank, so I just wrote it down.
    url='https://example.com/api'
    param = {
        'address': 'worker', #ID of the person you want to retrieve
        'type': 'json' #specifying data to retrieve
    }

    paramStr=urllib.parse.urlencode(param)
    requ=urllib.request.Request(f'{url}?{paramStr}')
    with urllib.request.urlopen(req) as response:
        return json.load(response)

res=dataGet()
#res is a dictionary type, so I print it after shaping it with json.Also, `ensure_ascii=False` prints non-ASCII characters as they are without escaping.
print(json.dumps(res,ensure_ascii=False,indent=2))


2022-09-30 21:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.