接口查询方法

import requests
import json
import traceback

"""接口查询"""
def GetSop(url,opid,queryvalue):
    """data_query_payload的配置根据接口文档来"""
    data_query_payload={
        "authcode":'111'
        "params":{
            "vttype":'a',
            "vt":vt
        }
    }
    """headers根据实际打开的网页头配置"""
    headers = {
        "Content-Type":"application/json"
    }
    try:
        """可以使用requests.post().json()来代替以下三行代码"""
        reponse = requests.post(url,headers=headers,data=json.dumps(data_query_payload))
        r = reponse.text
        queryjson = json.loads(r)
        res = queryjson['data']
    except:
        traceback.print_exc()
        res = 'error'
    return res

"""txt文件读取为列表"""
def TxtToList(infile):
    res = []
    with open(infile,'r') as f:
        for line in f.readlines():
            c = line.strip().strip('\n').split(',')
            res.extend(c)
    return res
 
"""数据存入txt文件"""
with open(outfile,'w') as f:
    res = GetSop(url,opid,queryvalue)
    if res!=[]:
        f.write(str(res)+'\n')
posted @ 2020-12-07 15:04  皮卡多之神  阅读(449)  评论(0)    收藏  举报