项目问题总结(2018-10-09)

一、python获取json数据

import urllib.request


def get_exchange_list():
    exchanges_list = json.loads(urllib.request.urlopen('http://q.botvs.net/api/symbols').read())
    return exchanges_list

报错:TypeError: the JSON object must be str, not 'bytes',意思为json对象必须为字符串类型,而不是字节类型

def get_exchange_list():
    exchanges_list = json.loads(urllib.request.urlopen('http://q.botvs.net/api/symbols').read().decode("utf-8"))
    return exchanges_list

问题解决

二、python ping ip

import os,datetime,time

def run():
    ip1 = "www.baidu.com"
    backinfo = os.system("ping -w 1 %s" % ip1)
    if backinfo != 0: 
        with open("app.txt", "a") as f:
            f.write(str(datetime.datetime.now()) + "--" + str(ip1) + "--" + str(backinfo) + "\n")
            f.close()


if __name__ == '__main__':
    while True:
        run()
        time.sleep(60)

你可能感兴趣的:(项目问题总结(2018-10-09))