Python如何獲得百度統(tǒng)計(jì)API的數(shù)據(jù)并發(fā)送郵件示例代碼
小工具
本來(lái)這么晚是不準(zhǔn)備寫博客的,當(dāng)是想到了那個(gè)狗子絕對(duì)會(huì)在開學(xué)的時(shí)候跟我逼逼這個(gè)事情,所以,還是老老實(shí)實(shí)地寫一下吧。
Baidu統(tǒng)計(jì)API的使用
系統(tǒng)環(huán)境:
Python2
- requests庫(kù):發(fā)出請(qǐng)求
- json庫(kù):json處理
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',
# 開始統(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
- requests庫(kù):發(fā)出請(qǐng)求
- json庫(kù):json處理
在這里,我使用的是SMTP協(xié)議去發(fā)送郵件,使用的是QQ郵箱,QQ郵箱的開啟,參考百度經(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è)人訪問了小主你的博客,其中獨(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à)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Python基于SMTP發(fā)送郵件的方法
- python實(shí)現(xiàn)發(fā)送郵件
- python使用Windows的wmic命令監(jiān)控文件運(yùn)行狀況,如有異常發(fā)送郵件報(bào)警
- python自動(dòng)化發(fā)送郵件實(shí)例講解
- python實(shí)現(xiàn)定時(shí)發(fā)送郵件到指定郵箱
- python實(shí)現(xiàn)定時(shí)發(fā)送郵件
- python腳本定時(shí)發(fā)送郵件
- python 發(fā)送郵件的示例代碼(Python2/3都可以直接使用)
- python 發(fā)送郵件的四種方法匯總
- 詳解python定時(shí)簡(jiǎn)單爬取網(wǎng)頁(yè)新聞存入數(shù)據(jù)庫(kù)并發(fā)送郵件
- Python發(fā)送郵件實(shí)現(xiàn)基礎(chǔ)解析
- Python 調(diào)用API發(fā)送郵件
相關(guān)文章
Python將xml和xsl轉(zhuǎn)換為html的方法
這篇文章主要介紹了Python將xml和xsl轉(zhuǎn)換為html的方法,實(shí)例分析了使用libxml2模塊操作xml和xsl轉(zhuǎn)換為html的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
Python:__eq__和__str__函數(shù)的使用示例
這篇文章主要介紹了Python:__eq__和__str__函數(shù)的使用示例,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-09-09
python使用watchdog實(shí)現(xiàn)文件資源監(jiān)控
watchdog 支持跨平臺(tái)文件資源監(jiān)控,可以檢測(cè)指定文件夾下文件及文件夾變動(dòng),下面我們來(lái)看看Python如何使用watchdog實(shí)現(xiàn)文件資源監(jiān)控吧2025-01-01
python xlwt如何設(shè)置單元格的自定義背景顏色
這篇文章主要介紹了python xlwt如何設(shè)置單元格的自定義背景顏色,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Python鼠標(biāo)事件及坐標(biāo)獲取窗口和屏幕坐標(biāo)
這篇文章主要介紹了Python編程中如何通過鼠標(biāo)事件及坐標(biāo)獲取窗口坐標(biāo)和屏幕坐標(biāo)的示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10

