利用Python3 获取百度统计数据,如果有数据异常则发邮件
如何获取站点id
查看报告的链接
# -*- 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