[Statistics] python reptile up every day over several main fan!

Statistics up every day over several main fan!

The first step, crawling up the main message of the fans

For convenience, here I write it as a function

1. First import package required

requests is essential!

import requests as req

Want to record statistics of time, then you need to use the appropriate function of time

from time import strftime, localtime

2. For convenience, it is written to a function

Accepts two parameters, namely up the Lord's name and custom mid, mid is the last string of numbers up personal space of the main web address bar. No custom name, it will automatically appear as mid, and do not worry about this point oh ~

def fans(mid, name=-1):
    mid = str(mid)
    name = str(name)
    if name == -1:
        name = mid
    url = "https://api.bilibili.com/x/relation/stat?vmid=" + mid + "&jsonp=jsonp"
    resp = req.get(url)# 通过url爬取到我们想要的json数据
    info = eval(resp.text)
    with open(name + '粉丝数统计.txt', 'a') as f:
        f.write(strftime("%Y", localtime()) + "年" + strftime("%m", localtime()) + "月" + strftime("%d",
                                                                                                 localtime()) + "日" + name + "粉丝数:" + str(
            info['data']['follower']) + '\n')# 获取data中的follower就是粉丝数啦
    print(strftime("%Y", localtime()) + "年" + strftime("%m", localtime()) + "月" + strftime("%d",
                                                                                           localtime()) + "日" + name + "粉丝数:" + str(
        info['data']['follower']) + '\n')

3. So this function is called about it!

if __name__ == "__main__":
    fans(36874384, '谜叔录播机')
    fans(673816, "谜之声")

4. Run it, the success of it!

Second, that good statistics do it again every day, one day I want to point again?

1. Of course not! Write a bat file Jiuhaola!

Whenever 0:00 will automatically execute python file again oh I gave it the name 'timer .bat', do not forget it has been hung, hee hee ~

if 0 equ %time:~0,2% (
python getP.py
)
timeout 3600

to sum up

Oh, very simple, everyone can try, statistics about the amount of fans change up your favorite Lord, I believe that it is an increasing function hee hee ~

Of course, it can not be limited to monitoring once a day, but more specifically, we all need to try it, I only count once a day ~

Guess you like

Origin www.cnblogs.com/zhangA/p/11280084.html