Python3 获取百度统计数据

利用Python3 获取百度统计数据,如果有数据异常则发邮件

如何获取站点id

查看报告的链接

Python
# -*- coding: utf-8 -*- """ @Time: 2018/6/11 @Author: songhao @微信公众号: zeropython @File: crawl_baidu_tongji.py """ import csv import json import time import datetime import urllib.parse import urllib.request from pprint import pprint from pandas import DataFrame from pprint import pprint from openpyxl import Workbook base_url = "https://api.baidu.com/json/tongji/v1/ReportService/getData" import datetime # 当前系统时间 nowTime=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') class Baidu(object): def __init__(self, siteId, username, password, token): self.siteId = siteId self.username = username self.password = password self.token = token def getresult(self, start_date, end_date, method, metrics, **kw): base_url = "https://api.baidu.com/json/tongji/v1/ReportService/getData" body = {"header": {"account_type": 1, "password": self.password, "token": self.token, "username": self.username}, "body": {"siteId": self.siteId, "method": method, "start_date": start_date, "end_date": end_date, "metrics": metrics}} for key in kw: body['body'][key] = kw[key] data = bytes(json.dumps(body), 'utf8') req = urllib.request.Request(base_url, data) response = urllib.request.urlopen(req) the_page = response.read() return the_page.decode("utf-8") # if __name__ == '__main__': # 初始花 csv def get_new_data(): wf = open('uvdata.csv','w',encoding='utf-8') writer = csv.writer(wf) writer.writerow(['时间', '栏目', '变化','uv1','uv2']) # ws.append(['时间', '栏目', '变化','uv1','uv2']) # 日期开始 today = datetime.date.today() yesterday = today - datetime.timedelta(days=1) fifteenago = today - datetime.timedelta(days=2) end, start = str(yesterday).replace("-", ""), str(fifteenago).replace("-", "") word_idc = [] weblist = {'新闻':648382,} for k,v in weblist.items(): # print(k,v) bd = Baidu(v, "用户名", "密码", "token") result = bd.getresult(start, end, "overview/getTimeTrendRpt", "visitor_count",max_results=100) data = json.loads(result).get('body').get('data')[0].get('result').get('items') data_time = [start,end] data_start_uv = data[1][0][0] data_end_uv = data[1][1][0] get_range_value = abs((data_start_uv-data_end_uv)/data_start_uv) if get_range_value >= 0.05: pre_data = '%.2f%%' % (get_range_value * 100) new_data =[data_time,k,pre_data,data_start_uv,data_end_uv] word_idc.append(new_data) return word_idc if __name__ == '__main__': get_new_data()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
"""
@Time: 2018/6/11
@Author: songhao
@微信公众号: zeropython
@File: crawl_baidu_tongji.py
"""
import csv
import json
import time
import datetime
import urllib . parse
import urllib . request
from pprint import pprint
from pandas import DataFrame
from pprint import pprint
from openpyxl import Workbook
 
base_url = "https://api.baidu.com/json/tongji/v1/ReportService/getData"
import datetime
 
# 当前系统时间
nowTime = datetime . datetime . now ( ) . strftime ( '%Y-%m-%d %H:%M:%S' )
 
class Baidu ( object ) :
     def __init__ ( self , siteId , username , password , token ) :
         self . siteId = siteId
         self . username = username
         self . password = password
         self . token = token
 
     def getresult ( self , start_date , end_date , method , metrics , * * kw ) :
         base_url = "https://api.baidu.com/json/tongji/v1/ReportService/getData"
         body = { "header" : { "account_type" : 1 , "password" : self . password , "token" : self . token ,
                           "username" : self . username } ,
                 "body" : { "siteId" : self . siteId , "method" : method , "start_date" : start_date ,
                         "end_date" : end_date , "metrics" : metrics } }
         for key in kw :
             body [ 'body' ] [ key ] = kw [ key ]
         data = bytes ( json . dumps ( body ) , 'utf8' )
         req = urllib . request . Request ( base_url , data )
         response = urllib . request . urlopen ( req )
         the_page = response . read ( )
         return the_page . decode ( "utf-8" )
 
 
# if __name__ == '__main__':
     # 初始花 csv
def get_new_data ( ) :
     wf = open ( 'uvdata.csv' , 'w' , encoding = 'utf-8' )
     writer = csv . writer ( wf )
     writer . writerow ( [ '时间' , '栏目' , '变化' , 'uv1' , 'uv2' ] )
 
     # ws.append(['时间', '栏目', '变化','uv1','uv2'])
 
     # 日期开始
     today = datetime . date . today ( )
     yesterday = today - datetime . timedelta ( days = 1 )
     fifteenago = today - datetime . timedelta ( days = 2 )
     end , start = str ( yesterday ) . replace ( "-" , "" ) , str ( fifteenago ) . replace ( "-" , "" )
     word_idc = [ ]
     weblist = { '新闻' : 648382 , }
     for k , v in weblist . items ( ) :
         # print(k,v)
         bd = Baidu ( v , "用户名" , "密码" , "token" )
         result = bd . getresult ( start , end , "overview/getTimeTrendRpt" ,
                               "visitor_count" , max_results = 100 )
         data = json . loads ( result ) . get ( 'body' ) . get ( 'data' ) [ 0 ] . get ( 'result' ) . get ( 'items' )
 
         data_time = [ start , end ]
         data_start_uv = data [ 1 ] [ 0 ] [ 0 ]
         data_end_uv = data [ 1 ] [ 1 ] [ 0 ]
         get_range_value = abs ( ( data_start_uv - data_end_uv ) / data_start_uv )
         if get_range_value >= 0.05 :
             pre_data = '%.2f%%' % ( get_range_value * 100 )
             new_data = [ data_time , k , pre_data , data_start_uv , data_end_uv ]
             word_idc . append ( new_data )
     return word_idc
 
if __name__ == '__main__' :
     get_new_data ( )
 
 

这段代码是 获取uv 数据,如果环比大于百分之5则发邮件通知站长,流量出现异常

如何发送邮件请移步
https://www.168seo.cn/python/24359.html




  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
课程背景:    企业里面很多系统管理后台,用easyui + highcharts + django 进行后台管理的整合,而这块资料在网上资料甚少,很多有经验的朋友在做这块时候也经常出现各种问题,目前我们老师以前在大数据真实项目中用到这块,现在受一些网友建议单独录制easyui + highcharts + django,希望可以帮助那些同学。视频+技术文档+源码让你看过后马上也可以搭建起来。  比如现在的友盟 和 百度统计 都是在类似这样的统计,我们会通过2~3个小时左右的课程,让你掌握这样的技术。   目标人群:  1、初级以上普通开发人员  2、web开发人员,对python感兴趣同学  3、对编程感兴趣的同学   课程目录:  01easyui_highcharts_django整合之需求介绍  02easyui_highcharts_django整合通过pycharm创建项目  03easyui_highcharts_django整合配置路由显示第一个页面  04easyui_highcharts_django整合下载easyui资源并且导入到项目里面  05easyui_highcharts_django整合修改配置文件加载static静态文件  06easyui_highcharts_django整合对页面进行修改  07easyui_highcharts_django整合对合并母模板layout  08easyui_highcharts_django整合添加一个新的业务的代码实现流程  09easyui_highcharts_django整合highcharts简单介绍  10easyui_highcharts_django整合highcharts的整合到django里面  11easyui_highcharts_django整合快速做出另一个业务流程  12easyui_highcharts_django整合柱状图  13easyui_highcharts_django后台返回json数据并且展示   14大数据django后台数据保存到mysql里面  15大数据django从mysql里面读取数据并显示到页面上面  课程环境:    环境:win10,  python 2.7.13,  django 1.8.3 ,  pymsql   收获预期:    1.使用django开发项目  2.学会使用easyui框架  3.学会使用highcharts  4.easyui_highcharts_django开发后面展示项目   案例截图:      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值