Json-RPC 2.0-如何提取数据

时间:2018-09-21 10:26:42

标签: python json json-rpc

我正在尝试从内部项目(http:\ 192.168.1.15:8082)中提取数据。它使用json-rpc 2.0。刚开始,我尝试使用beautilsoup检索信息,因为该信息正在屏幕上显示,但是那行不通,我得到的只是一堆函数代码。我对json-rpc不熟悉。欢迎任何帮助。

网站上打印的REPONSE数据如下:

响应:

{"jsonrpc":"2.0","result":[{"uri":"Geometry","time":1537525006,"geo":{"type":"characteristic","id":125,"information2":{"type":"Point","Weight":[15.362154,196.623546]}

如何检索此信息?

谢谢大家

尼克,

1 个答案:

答案 0 :(得分:0)

看看requests https://pypi.org/project/requests

要遵守标准,您需要在请求中加入payload,否则只是普通的post

import requests
import json

def main():
    url = "http://192.168.1.15:8082/jsonrpc"
    headers = {'content-type': 'application/json'}

    # Example echo method
    payload = {
        "method": "your_method_name",
        "jsonrpc": "2.0",
        "id": 0,
    }
    response = requests.post(
        url, data=json.dumps(payload), headers=headers).json()

    print(response)
    print(response['result']['uri'])
    print(response['result']['time'])
    print(response['result']['geo'])
    print(response['result']['geo']['id'])
    print(response['result']['geo']['type'])
    print(response['result']['geo']['information2'])
    print(response['result']['geo']['information2']['type'])
    print(response['result']['geo']['information2']['Weight'])

if __name__ == "__main__":
    main()

如果您想要特定于json_rpc的内容,请查看jsonrpc_requests https://pypi.org/project/jsonrpc-requests/
请注意,在本教程中,如何为您处理响应的反序列化,您只是获取方法调用的结果,而不是整个响应。

from jsonrpc_requests import Server

def main():
    url = "http://localhost:8082/jsonrpc"
    server = Server(url)
    result  = server.dosomething()


    print(result)
    print(result['uri'])
    print(result['time'])
    print(result['geo'])
    print(result['geo']['id'])
    print(result['geo']['type'])
    print(result['geo']['information2'])
    print(result['geo']['information2']['type'])
    print(result['geo']['information2']['Weight'])

if __name__ == "__main__":
    main()