您好,登錄后才能下訂單哦!
小工具
本來(lái)這么晚是不準(zhǔn)備寫博客的,當(dāng)是想到了那個(gè)狗子絕對(duì)會(huì)在開(kāi)學(xué)的時(shí)候跟我逼逼這個(gè)事情,所以,還是老老實(shí)實(shí)地寫一下吧。
Baidu統(tǒng)計(jì)API的使用
系統(tǒng)環(huán)境:
Python2
getSiteList的使用
官方文檔在此,說(shuō)實(shí)話,這是我使用百BaiduAPI最坑的一次,在這個(gè)官方文檔的getSiteList中,完全不告訴你請(qǐng)求參數(shù)是什么。
首先,需要獲得百度統(tǒng)計(jì)API的token,在這里寫了token獲得的流程。
# encoding=utf-8 import requests import json siteListUrl = "https://api.baidu.com/json/tongji/v1/ReportService/getSiteList" # 這個(gè)是請(qǐng)求的數(shù)據(jù) data = { "header": { 'username': "你的用戶名", 'password': "你的密碼", 'token': '前面所獲得的token', 'Content-type': 'application/json' } } # 把請(qǐng)求數(shù)據(jù)變成json數(shù)據(jù) data = json.dumps(data) r = requests.post(url,data=data) # 在返回的信息中包含了網(wǎng)站的id等等,這些官方有說(shuō)明 print r.text
getData的使用
# 假設(shè)我的網(wǎng)站的ID是:12914021, getDataUrl = "https://api.baidu.com/json/tongji/v1/ReportService/getData" # 請(qǐng)求數(shù)據(jù)如下 data = { "header": { 'username': "你的用戶名", 'password': "你的密碼", 'token': '前面所獲得的token', 'Content-type': 'application/json' }, # 這個(gè)body的請(qǐng)求參數(shù)可以去參考官方說(shuō)明,在這里我只是想獲取pv和uv的數(shù)據(jù) "body": { 'site_id': 12914021, 'method': 'trend/time/a', # 開(kāi)始統(tǒng)計(jì)時(shí)間 'start_date': '20190125', # 結(jié)束統(tǒng)計(jì)時(shí)間 'end_date': '20190126', # 獲得pv和uv數(shù)據(jù) '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] # 頁(yè)面瀏覽量 pv = pv_uv[0] # 獨(dú)立訪客數(shù) uv = pv_uv[1] print pv_uv # 例如[120,100]
此時(shí),我們就已經(jīng)獲得了pv和nv的數(shù)據(jù)。
使用Python發(fā)送郵件
Python2
在這里,我使用的是SMTP協(xié)議去發(fā)送郵件,使用的是QQ郵箱,QQ郵箱的開(kāi)啟,參考百度經(jīng)驗(yàn)。
from email.mime.text import MIMEText from email.header import Header from smtplib import SMTP_SSL # qq郵箱smtp服務(wù)器 hostServer = 'smtp.qq.com' # 發(fā)送者的郵箱 sendMail = '你的QQ郵箱' receiveMail = '接收方的郵件地址' # ssl登錄 smtp = SMTP_SSL(hostServer) # 發(fā)送者的QQ,以及授權(quán)碼 smtp.login('你的qq', '授權(quán)碼') # plain代表發(fā)送為文本 msg = MIMEText("你要發(fā)送的內(nèi)容", "plain", 'utf-8') # 發(fā)送的標(biāo)題 msg["Subject"] = Header("帥哥的郵件", 'utf-8') # 發(fā)送方 msg["From"] = sendMail # 接收方 msg["To"] = receiveMail # 發(fā)送郵件 smtp.sendmail(sendMail, receiveMail, msg.as_string()) # 退出 smtp.quit()
結(jié)合使用
代碼寫的耦合度比較高,如果使用的話,需要根據(jù)自己的實(shí)際情況去修改
# 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 # 獲得時(shí)間 格式為:【20190125】 nowTime = time.strftime("%Y%m%d", time.localtime()) # 發(fā)送方的QQ sendQQ = "xxx" # 接收方的郵件地址 receiveMail = "xxx" # 百度統(tǒng)計(jì)token token = "xxx" # 需要查詢的網(wǎng)站id siteId = xxx # qq郵箱授權(quán)碼 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): # 郵件的正文內(nèi)容 mailContent = "小主,晚上好,這是昨天的統(tǒng)計(jì)數(shù)據(jù),昨天的博客園一共有%s個(gè)人訪問(wèn)了小主你的博客,其中獨(dú)立訪客有%s位。\n小主你要加油寫博客哦,有朝一日,你總會(huì)成為大佬的!(*^__^*) 嘻嘻……" % (pv_uv[0],pv_uv[1]) # qq郵箱smtp服務(wù)器 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("博客園統(tǒng)計(jì)郵件", 'utf-8') msg["From"] = sendEmail msg["To"] = receiveMail smtp.sendmail(sendEmail, receiveMail, msg.as_string()) smtp.quit() sendMail(get_pv_uv())
這時(shí)候,我們就可以將我們的python程序部署在Linux云服務(wù)器上面,那么我們?cè)趺茨軌蜃屵@個(gè)程序在每天的23.30分運(yùn)行呢?這時(shí)候我們就可以使用Linux上面的crontab了。
進(jìn)入linux,輸入crontab -e,然后在里面30 23 * * * python ~/Home/#py【你的Python文件地址】 >> #txt就可以設(shè)置為,在晚上的11.30分發(fā)送該郵件。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。