当前位置: 移动技术网 > IT编程>脚本编程>Python > Python如何获得百度统计API的数据并发送邮件示例代码

Python如何获得百度统计API的数据并发送邮件示例代码

2019年03月18日  | 移动技术网IT编程  | 我要评论

小柠檬早教网,大都市单身白领联谊会,电影苹果下载

小工具

本来这么晚是不准备写博客的,当是想到了那个狗子绝对会在开学的时候跟我逼逼这个事情,所以,还是老老实实地写一下吧。

baidu统计api的使用

系统环境:

python2

  • requests库:发出请求
  • json库:json处理

getsitelist的使用

官方文档在此,说实话,这是我使用百baiduapi最坑的一次,在这个官方文档的getsitelist中,完全不告诉你请求参数是什么。

首先,需要获得百度统计api的token,在写了token获得的流程。

# encoding=utf-8
import requests
import json

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等等,这些官方有说明
print r.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]

print pv_uv # 例如[120,100]

此时,我们就已经获得了pv和nv的数据。

使用python发送邮件

python2

  • requests库:发出请求
  • json库:json处理

在这里,我使用的是smtp协议去发送邮件,使用的是qq邮箱,qq邮箱的开启,参考。

from email.mime.text import mimetext
from email.header import header
from smtplib import smtp_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
import time
import requests
import json
from email.mime.text import mimetext
from email.header import header
from smtplib import smtp_ssl

# 获得时间 格式为:【20190125】
nowtime = time.strftime("%y%m%d", time.localtime())
# 发送方的qq
sendqq = "xxx"
# 接收方的邮件地址
receivemail = "xxx"
# 百度统计token
token = "xxx"
# 需要查询的网站id
siteid = xxx
# qq邮箱授权码
mailcode = "xxx"


def get_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]
 return pv_uv


def sendmail(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,然后在里面30 23 * * * python ~/home/tongji.py【你的python文件地址】 >> tongji.txt就可以设置为,在晚上的11.30分发送该邮件。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网