利用API抓取数据入库

import datetime
import json
import re
import time
from threading import Timer

import requests
import pymysql
db = pymysql.connect(“000000000”, “root”, “xxxxx”, “xxxx”)
cursor = db.cursor()

def print_time():
while True:
print(‘Job1-startTime:%s’ % (datetime.datetime.now().strftime(’%Y-%m-%d %H:%M:%S’)))
w = time.time()
url = “xxxxxxxxxxxxxxxxx”
headers = {‘Content-Type’: ‘application/json’}
parm = {
“command”: “xxxxxxxxxxxxxxxxx”,
“switch_ip”: “ip”
}

    red = requests.post(url, headers=headers, json=parm)

    red = red.content.decode()

    data = json.loads(red)
    hello = data['Data']['config']
    s = hello.splitlines()
    x = []
    for d in s:
        if "Output:" and "bytes" in d:

            s = re.findall("\d+", d)[0]

            x.append(s)

    y = x[0]
    d = x[1]
    k = time.time()

    insert_switch = ("INSERT INTO NB(input,output,time)" "VALUES(%s,%s,%s)")
    data_switch = (y, d, k)
    cursor.execute(insert_switch, data_switch)
    db.commit()

    print(y, d, k)

    r = time.time() - w
    slp = 60 - r
    time.sleep(slp)
    print('Job1-endTime:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
    print('------------------------------------------------------------------------')

def main():
# ip = str(input(“请输入交换机IP:”)) #
time_start = int(input(“请输入多久后执行(单位/秒):”))
print_time()
Timer(time_start, print_time, ).start()
db.close()

if name == ‘main’:
main()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值