import time, requests TOKEN = ""
def Tongji(TOKEN): TIME =time.strftime("%Y%m%d", time.localtime())
def Get_UV(TOKEN,ID,TIME): A = "https://openapi.baidu.com/rest/2.0/tongji/report/getData?access_token=" + TOKEN B = "&site_id="+str(ID) C = "&start_date=20200120&end_date="+TIME D = "&metrics=pv_count%2Cvisitor_count&method=overview%2FgetTimeTrendRpt" url = A+B+C+D Data = requests.get(url).json() UVPV_list = Data['result']['items'][1] PV_T = UVPV_list[-1][0] if PV_T =="--": PV_T = 0 UV_T = UVPV_list[-1][1] if UV_T =="--": UV_T = 0 PV = [] UV = [] for i in UVPV_list: if i[0] != "--": PV+=[i[0]] if i[1] != "--": UV+=[i[1]] PV = str(sum(PV))+"+"+str(PV_T) UV = str(sum(UV))+"+"+str(UV_T) return PV,UV
def Location_Get(TOKEN, TIME): url1 = "https://openapi.baidu.com/rest/2.0/tongji/report/getData?access_token=" url2 = TOKEN +"&site_id=14350939&start_date=" url3 = TIME + "&end_date="+TIME+"&metrics=pv_count&method=overview%2FgetDistrictRpt" url = url1+url2+url3 Data = requests.get(url).json() Location = "" for i in Data['result']['items'][0]: Location += i[0] +'\n' Location_N = "" for i in Data['result']['items'][1]: Location_N += str(i[0]) +'\n' return Location,Location_N
def Location_Get_C(TOKEN, TIME): url1 = "https://openapi.baidu.com/rest/2.0/tongji/report/getData?access_token=" url2 = TOKEN +"&site_id=14350939&start_date=" url3 = TIME + "&end_date="+TIME+"&metrics=pv_count&method=visit%2Fworld%2Fa" url = url1+url2+url3 Data = requests.get(url).json() Location = "" for i in Data['result']['items'][0]: Location += i[0]['name'] +'\n' Location_N = "" for i in Data['result']['items'][1]: Location_N += str(i[0]) +'\n' return Location,Location_N
def New_Visitor_Get(TOKEN, TIME): url1 = "https://openapi.baidu.com/rest/2.0/tongji/report/getData?access_token=" url2 = TOKEN + "&site_id=14350939&start_date=" url3 = TIME + "&end_date=" + TIME + "&metrics=new_visitor_ratio&method=source%2Fall%2Fa" url = url1 + url2 +url3 Data = requests.get(url).json() return "新訪客比例 "+str(Data['result']['sum'][0][0])+"%"
def main(): url1= "https://openapi.baidu.com/rest/2.0/tongji/config/getSiteList?access_token="+TOKEN List = requests.get(url1).json() Domain = List['list'][0]['domain'] Domain_ID = List['list'][0]['site_id'] Sub_domain = [[Domain,Domain_ID]] Sub_List = List['list'][0]['sub_dir_list'] for i in Sub_List: Sub_domain += [[i['name'],i['sub_dir_id']]] Result = {} for i in Sub_domain: PV,UV = Get_UV(TOKEN,i[1],TIME) Result.update({i[0]:{"PV":PV,"UV":UV}}) Name = "" PV = "" UV = "" for i in Result: Name += i + "\n" PV += Result[i]['PV']+"\n" UV += Result[i]['UV']+"\n" New_Vis_N = New_Visitor_Get(TOKEN, TIME) Location,Location_N = Location_Get(TOKEN, TIME) Location_C,Location_C_N = Location_Get_C(TOKEN, TIME) return Name,PV,UV,Location,Location_N,Location_C,Location_C_N,New_Vis_N
Name,PV,UV,Location,Location_N,Location_C,Location_C_N,New_Vis_N = Tongji(TOKEN)
|