Python如何获得百度统计API的数据并发送邮件示例代码
小工具
本来这么晚是不准备写博客的,当是想到了那个狗子绝对会在开学的时候跟我逼逼这个事情,所以,还是老老实实地写一下吧。
Baidu统计API的使用
系统环境:
Python2
- requests库:发出请求
- json库:json处理
getSiteList的使用
官方文档在此,说实话,这是我使用百BaiduAPI最坑的一次,在这个官方文档的getSiteList中,完全不告诉你请求参数是什么。
首先,需要获得百度统计API的token,在这里写了token获得的流程。
#encoding=utf-8
importrequests
importjson
siteListUrl="https://api.baidu.com/json/tongji/v1/ReportService/getSiteList"
#这个是请求的数据
data={
"header":{
'username':"你的用户名",
'password':"你的密码",
'token':'前面所获得的token',
'Content-type':'application/json'
}
}
#把请求数据变成json数据
data=json.dumps(data)
r=requests.post(url,data=data)
#在返回的信息中包含了网站的id等等,这些官方有说明
printr.text
getData的使用
#假设我的网站的ID是:12914021,
getDataUrl="https://api.baidu.com/json/tongji/v1/ReportService/getData"
#请求数据如下
data={
"header":{
'username':"你的用户名",
'password':"你的密码",
'token':'前面所获得的token',
'Content-type':'application/json'
},
#这个body的请求参数可以去参考官方说明,在这里我只是想获取pv和uv的数据
"body":{
'site_id':12914021,
'method':'trend/time/a',
#开始统计时间
'start_date':'20190125',
#结束统计时间
'end_date':'20190126',
#获得pv和uv数据
'metrics':'pv_count,visitor_count'
}
}
r=requests.post(getDataUrl,data=json.dumps(data))
result=json.loads(r.text)
pv_uv=result["body"]["data"][0]["result"]["pageSum"][0]
#页面浏览量
pv=pv_uv[0]
#独立访客数
uv=pv_uv[1]
printpv_uv#例如[120,100]
此时,我们就已经获得了pv和nv的数据。
使用Python发送邮件
Python2
- requests库:发出请求
- json库:json处理
在这里,我使用的是SMTP协议去发送邮件,使用的是QQ邮箱,QQ邮箱的开启,参考百度经验。
fromemail.mime.textimportMIMEText
fromemail.headerimportHeader
fromsmtplibimportSMTP_SSL
#qq邮箱smtp服务器
hostServer='smtp.qq.com'
#发送者的邮箱
sendMail='你的QQ邮箱'
receiveMail='接收方的邮件地址'
#ssl登录
smtp=SMTP_SSL(hostServer)
#发送者的QQ,以及授权码
smtp.login('你的qq','授权码')
#plain代表发送为文本
msg=MIMEText("你要发送的内容","plain",'utf-8')
#发送的标题
msg["Subject"]=Header("帅哥的邮件",'utf-8')
#发送方
msg["From"]=sendMail
#接收方
msg["To"]=receiveMail
#发送邮件
smtp.sendmail(sendMail,receiveMail,msg.as_string())
#退出
smtp.quit()
结合使用
代码写的耦合度比较高,如果使用的话,需要根据自己的实际情况去修改
#encoding=utf-8
importtime
importrequests
importjson
fromemail.mime.textimportMIMEText
fromemail.headerimportHeader
fromsmtplibimportSMTP_SSL
#获得时间格式为:【20190125】
nowTime=time.strftime("%Y%m%d",time.localtime())
#发送方的QQ
sendQQ="xxx"
#接收方的邮件地址
receiveMail="xxx"
#百度统计token
token="xxx"
#需要查询的网站id
siteId=xxx
#qq邮箱授权码
mailCode="xxx"
defget_pv_uv():
dataUrl="https://api.baidu.com/json/tongji/v1/ReportService/getData"
body={
"header":{
'username':"xxx",
'password':"xxx",
'token':token,
'Content-type':'application/json'
},
"body":{
'site_id':siteId,
'method':'trend/time/a',
'start_date':nowTime,
'end_date':nowTime,
'metrics':'pv_count,visitor_count'
}
}
r=requests.post(dataUrl,data=json.dumps(body))
result=json.loads(r.text)
pv_uv=result["body"]["data"][0]["result"]["pageSum"][0]
returnpv_uv
defsendMail(pv_uv):
#邮件的正文内容
mailContent="小主,晚上好,这是昨天的统计数据,昨天的博客园一共有%s个人访问了小主你的博客,其中独立访客有%s位。\n小主你要加油写博客哦,有朝一日,你总会成为大佬的!(*^__^*)嘻嘻……"%(pv_uv[0],pv_uv[1])
#qq邮箱smtp服务器
hostServer='smtp.qq.com'
sendEmail=sendQQ+'@qq.com'
#ssl登录
smtp=SMTP_SSL(hostServer)
smtp.login(sendQQ,mailCode)
msg=MIMEText(mailContent,"plain",'utf-8')
msg["Subject"]=Header("博客园统计邮件",'utf-8')
msg["From"]=sendEmail
msg["To"]=receiveMail
smtp.sendmail(sendEmail,receiveMail,msg.as_string())
smtp.quit()
sendMail(get_pv_uv())
这时候,我们就可以将我们的python程序部署在Linux云服务器上面,那么我们怎么能够让这个程序在每天的23.30分运行呢?这时候我们就可以使用Linux上面的crontab了。
进入linux,输入crontab-e,然后在里面3023***python~/Home/tongji.py【你的Python文件地址】>>tongji.txt就可以设置为,在晚上的11.30分发送该邮件。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。