Loading

soarli

简易Python爬虫实时监视校园网当月流量信息
效果代码import requests import os import re import time whil...
扫描右侧二维码阅读全文
26
2021/05

简易Python爬虫实时监视校园网当月流量信息

效果

代码

import requests
import os
import re
import time

while 1:
    html = open('get.html','w+')
    r = requests.get(url='http://10.0.108.3')
    print(r.text,file=html)

    html.close()

    html = open('get.html','r')

    for i in range(13):
        line = html.readline()
        #print(line)

    zz = re.findall(r"flow=\'(.*)\';f", line)

    ti_qu_de_jie_guo = zz[0]
    jieguo = int(ti_qu_de_jie_guo)/1024/1024


    print("当前网络环境本月用了:"+ str(round(jieguo, 2)) + "GB流量!", end="\r")
    time.sleep(0.3)

5.29更新

27日晚上突发奇想,尝试将结果输出到网页上以实现校园网范围内都可以看到实时宿舍上网流量信息(貌似没太大用),但是这种前后端分离的思想倒是得到了一次充分的实践。

import requests
import os
import re
import time

def sc_html():
    print('<!DOCTYPE html>',file=index)
    print('<html>',file=index)
    print('<head>',file=index)
    print('    <title>网络流量信息查看 - Powered by soarli</title>',file=index)
    print('    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">',file=index)
    print('    <link rel="icon" href="soarli.ico">',file=index)
    print('    <style type="text/css">',file=index)
    print('        body{',file=index)
    print('            background-image: url("bj.jpg");',file=index)
    print('            background-repeat: repeat;',file=index)
    print("            cursor:url('https://cdn2.soarli.top/usr/plugins/HoerMouse/static/image/dew/normal.cur'), auto;",file=index)
    print('        }',file=index)
    print('        #info{',file=index)
    print('            text-align: center;',file=index)
    print('            font-size: 24px;',file=index)
    print('        }',file=index)
    print('    </style>',file=index)
    print('    <script language="JavaScript"> ',file=index)
    print('    function myrefresh() ',file=index)
    print('    { ',file=index)
    print('    window.location.reload(); ',file=index)
    print('    } ',file=index)
    print("    setTimeout('myrefresh()',5000); ",file=index)
    print('    </script> ',file=index)
    print('</head>',file=index)
    print('<body>',file=index)
    print('<br><br><br><br>',file=index)
    print('<div id="info">',file=index)
    print('<img src="soarli.ico"><br><br>',file=index)
    print("我的网络环境本月用了:"+ str(round(jieguo, 2)) + "GB流量!",file=index)
    print('<br><br>本页面每5秒钟自动刷新一次...',file=index)
    print('</div>',file=index)
    print('</body>',file=index)
    print('</html>',file=index)
    
while 1:
    html = open('get.html','w+')
    index = open('index.html','w')
    r = requests.get(url='http://10.0.108.3')
    print(r.text,file=html)

    html.close()

    html = open('get.html','r')

    for i in range(13):
        line = html.readline()
        #print(line)

    zz = re.findall(r"flow=\'(.*)\';f", line)

    ti_qu_de_jie_guo = zz[0]
    jieguo = int(ti_qu_de_jie_guo)/1024/1024


    sc_html()
    #index.close()
    time.sleep(0.3)

正则表达式在线测试

https://tool.oschina.net/regex/

参考文章:

https://blog.csdn.net/ztf312/article/details/47259805

https://www.runoob.com/regexp/regexp-syntax.html

https://blog.csdn.net/lcczzu/article/details/46879683

https://blog.csdn.net/Jerry_1126/article/details/85009810

https://zhuanlan.zhihu.com/p/33288426

最后修改:2021 年 05 月 29 日 05 : 01 PM

2 条评论

  1. HMP

    天天追更老哥博客

    1. soarli

发表评论