Python如何獲得百度統(tǒng)計(jì)API的數(shù)據(jù)并發(fā)送郵件示例代碼
小工具
本來(lái)這么晚是不準(zhǔn)備寫(xiě)博客的,當(dāng)是想到了那個(gè)狗子絕對(duì)會(huì)在開(kāi)學(xué)的時(shí)候跟我逼逼這個(gè)事情,所以,還是老老實(shí)實(shí)地寫(xiě)一下吧。
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,在這里寫(xiě)了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
- requests庫(kù):發(fā)出請(qǐng)求
- json庫(kù):json處理
在這里,我使用的是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é)合使用
代碼寫(xiě)的耦合度比較高,如果使用的話,需要根據(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小主你要加油寫(xiě)博客哦,有朝一日,你總會(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ì)腳本之家的支持。
- 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實(shí)現(xiàn)簡(jiǎn)單爬取網(wǎng)頁(yè)數(shù)據(jù)并導(dǎo)入MySQL中的數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了如何使用 python 實(shí)現(xiàn)簡(jiǎn)單爬取網(wǎng)頁(yè)數(shù)據(jù)并導(dǎo)入 MySQL 中的數(shù)據(jù)庫(kù),對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-06-06關(guān)于Python字典(Dictionary)操作詳解
這篇文章主要介紹了關(guān)于Python字典(Dictionary)操作詳解,Python字典是另一種可變?nèi)萜髂P?,且可存?chǔ)任意類型對(duì)象,如字符串、數(shù)字、元組等其他容器模型,需要的朋友可以參考下2023-04-04解決TypeError: Object of type xxx is&
這篇文章主要介紹了解決TypeError: Object of type xxx is not JSON serializable錯(cuò)誤問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Python實(shí)現(xiàn)字符串格式化的方法小結(jié)
本篇文章主要介紹了Python實(shí)現(xiàn)字符串格式化的方法小結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02PyCharm無(wú)法識(shí)別PyQt5的2種解決方法,ModuleNotFoundError: No module named
這篇文章主要介紹了PyCharm無(wú)法識(shí)別PyQt5的兩種解決辦法,ModuleNotFoundError: No module named 'pyqt5',需要的朋友可以參考下2020-02-02基于python traceback實(shí)現(xiàn)異常的獲取與處理
這篇文章主要介紹了基于python traceback實(shí)現(xiàn)異常的獲取與處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12python opencv檢測(cè)直線 cv2.HoughLinesP的實(shí)現(xiàn)
cv2.HoughLines()函數(shù)是在二值圖像中查找直線,本文結(jié)合示例詳細(xì)的介紹了cv2.HoughLinesP的用法,感興趣的可以了解一下2021-06-06