獲取博客園本人的積分排名數據:mvc
1. 抓包獲取積分排名數據返回接口:http://www.cnblogs.com/belle-ls/mvc/blog/sidecolumn.aspx?blogApp=belle-lside
2. 解析返回的數據,獲取積分排名url
3. 開啓線程,循環打印積分排名信息spa
from bs4 import BeautifulSoup import time import requests import threading class BlogRankMonitor(object): """ 獲取博客園積分排名 """
def __init__(self, id): self.urlBasic = 'http://www.cnblogs.com/%s/mvc/blog/sidecolumn.aspx?blogApp=%s' self.id = id self.score = 0 self.rank = 0 def get_nums(self, blogs_des): """ 分割字符串 積分 - 43 排名 - 283325,獲取數字 :return: """ split_str = blogs_des.split('-')[1].strip() return split_str def get_blog_ranks(self): """ 解析頁面獲取博客積分和排名 :return: """ url = self.urlBasic % (self.id, self.id) res = requests.get(url) soup = BeautifulSoup(res.text, "lxml") liScore= soup.find(class_="liScore") for child in liScore.children: if u'積分' in child.string: self.score = self.get_nums(child.string) elif u'排名' in child.string: self.rank = self.get_nums(child.string) print("積分:", blog.score, "排名:", blog.rank, "時間:", time.strftime("%Y-%m-%d %X", time.localtime())) def start_score_rank_thread(self): t = threading.Thread(target= self.get_blog_ranks()) t.start() if __name__ == '__main__': blog = BlogRankMonitor('belle-ls') while 1: blog.start_score_rank_thread() time.sleep(10) #10s打印一次
""" 運行結果: 積分: 45 排名: 0 時間: 2019-02-12 15:57:22 積分: 45 排名: 0 時間: 2019-02-12 15:57:32 積分: 45 排名: 0 時間: 2019-02-12 15:57:42 積分: 45 排名: 0 時間: 2019-02-12 15:57:53 積分: 45 排名: 0 時間: 2019-02-12 15:58:03 .... """