python爬虫模拟登陆校园网+连接校园wifi
注:本文僅作為學(xué)術(shù)交流和技術(shù)分析,所有的敏感信息全部打碼,登錄的賬號(hào)為本人自己的賬號(hào),不涉及任何敏感行為,轉(zhuǎn)載請(qǐng)注明
因本人在學(xué)校學(xué)習(xí)期間每次開(kāi)機(jī),都需要連接校園的wifi(學(xué)校的wifi在每次連接時(shí),都需要向彈出網(wǎng)頁(yè)填寫(xiě)數(shù)據(jù),提交表單),覺(jué)得操作繁瑣,學(xué)習(xí)的爬蟲(chóng)課程剛好派上用場(chǎng)。想著使用POST請(qǐng)求去模擬登陸并實(shí)現(xiàn)連接校園WIFI。最終實(shí)現(xiàn):計(jì)算機(jī)開(kāi)機(jī)自動(dòng)啟動(dòng)腳本,進(jìn)行網(wǎng)絡(luò)(指定WIFI)連接。
首先、進(jìn)入校園網(wǎng)的登錄界面
按下F12鍵 打開(kāi)控制臺(tái),點(diǎn)擊登錄,通過(guò)抓包發(fā)現(xiàn)這條請(qǐng)求auth_action.php
通過(guò)抓包發(fā)現(xiàn)為post請(qǐng)求,所以對(duì)需要提交的表單數(shù)據(jù)進(jìn)行研究,身份類型有電信、聯(lián)通、移動(dòng)等,他們所對(duì)應(yīng)的表單略有不同
?
?發(fā)現(xiàn)提交表單的數(shù)據(jù)中username欄中最后都會(huì)有一個(gè)@+字符,分別代表著三大通信運(yùn)營(yíng)商
爬蟲(chóng)思路:使用Python的requests模塊中的POST方法提交頁(yè)面需要的表單到網(wǎng)站上。頁(yè)面給予反饋。提示登錄成功。
代碼如下:
import requests import base64 ? # 網(wǎng)址 url="http://此處打碼/include/auth_action.php" headers = {'User-Agent': '自己電腦的請(qǐng)求頭','Referer': 'http://此處打碼/srun_portal_pc.php?ac_id=2&url=此處打碼', } username = "學(xué)號(hào)" # 測(cè)試學(xué)號(hào) password = "123456" # 測(cè)試密碼 # 對(duì)密碼進(jìn)行加密 pwd = base64.b64encode(password.encode("utf-8")).decode("ascii") ? type_phone = {"電信": "@telecom","移動(dòng)": "@cmcc","聯(lián)通": "@unicom",# @telecom表示電信 @cmcc表示移動(dòng) @unicom表示聯(lián)通 } query_date = {'action': 'login','username': username + type_phone["電信"],'password': '{B}' + pwd, ?# 發(fā)現(xiàn)最后密碼這里使用了base64加密,所以需要使用base64模塊對(duì)密碼進(jìn)行加密'ac_id': '2','user_ip': '','nas_ip': '','user_mac': '','save_me': '0','ajax': '1', } response = requests.post(url=url,headers=headers,data=query_date).content.decode("utf-8") print(response)模擬登陸連接校園WIFI功能已實(shí)現(xiàn)。但是!但是!再執(zhí)行腳本的時(shí)候,發(fā)現(xiàn)并不是想象中的順利,現(xiàn)在的腳本。啟動(dòng)后并不能進(jìn)行連接WIFI。原因是:沒(méi)有人為點(diǎn)擊網(wǎng)絡(luò)選擇連接指定的wifi,上述代碼中的網(wǎng)頁(yè)彈不出來(lái)。
于是本人就開(kāi)始了漫長(zhǎng)的“百度”,發(fā)現(xiàn):
博主:Mmagic1 的文章 用python來(lái)控制wifi連接
lizz2276 的文章 python連接wifi的模塊--pywifi介紹
介紹了對(duì)應(yīng)的方法。代碼如下:(截取了該腳本需要的代碼片段)
注:下方代碼來(lái)源于Mmagic1 與 lizz2276 博主
#-*-coding:utf-8-*- # 連接WIFI import pywifi,time from pywifi import constdef wifi_connect_status():wifi = pywifi.PyWiFi()iface = wifi.interfaces()[0] #acquire the first Wlan card,maybe notif iface.status() in [const.IFACE_CONNECTED,const.IFACE_INACTIVE]:print("已連接wifi!")return 1else:print("沒(méi)有連接到wifi!")return 0def connect_wifi():wifi = pywifi.PyWiFi()ifaces = wifi.interfaces()[0]print(ifaces.name()) ? ? ? ? ? ? ? #輸出無(wú)線網(wǎng)卡名稱ifaces.disconnect()time.sleep(3)profile = pywifi.Profile() ? ? ? ? ? ? ? ? ? ? ? ? ?#配置文件profile.ssid = "校園網(wǎng)WIFI" ? ? ? ? ? ? ? ? ? ? ? ?#wifi名稱# 因?yàn)樾@網(wǎng)在點(diǎn)擊連接的時(shí)候并不像其他WIFI一樣直接輸入密碼。所以我注釋掉了下面一部分代碼#profile.auth = const.AUTH_ALG_OPEN ? ? ? ? ? ? ? ? #需要密碼#profile.akm.append(const.AKM_TYPE_WPA2PSK) ? ? ? ? #加密類型#profile.cipher = const.CIPHER_TYPE_CCMP ? ? ? ? ? ? #加密單元#profile.key = "88888888" ? ? ? ? ? ? ? ? ? ? ? ? ? #wifi密碼ifaces.remove_all_network_profiles() ? ? ? ? ? ? ? ?#刪除其它配置文件tmp_profile = ifaces.add_network_profile(profile) ? #加載配置文件ifaces.connect(tmp_profile)time.sleep(5)isok = Trueif ifaces.status() == const.IFACE_CONNECTED:print("連接成功!")else:print("連接失敗!")time.sleep(1)return isokdef main():print("start")wifi_connect_status()connect_wifi()print("finish!")if __name__ == "__main__":main() ? # 原文鏈接:https://blog.csdn.net/Mmagic1/article/details/120066894可以實(shí)現(xiàn)打開(kāi)wifi并點(diǎn)擊的功能
模塊pywifi在使用的時(shí)候可能會(huì)出現(xiàn)下面報(bào)錯(cuò)
報(bào)錯(cuò)ModuleNotFoundError: No module named ‘comtypes‘
解決辦法:只需要將報(bào)錯(cuò)信息中的模塊comtypes,進(jìn)行下載即可
參考:碼詩(shī)~ 文章 導(dǎo)入pywifi時(shí),報(bào)錯(cuò)ModuleNotFoundError: No module named ‘comtypes‘
最終代碼展示:
import requests import base64 import pywifi, time from pywifi import const ? """ 首先、安裝 comtypes,pywifi,requests,pyinstaller 模塊pip install pywifipip install requestspip install comtypespip install pyinstaller 其次、編輯self_date 修改字典中的數(shù)據(jù)。 執(zhí)行腳本。 """ ? ? class Login_Wifi(object): ?def __init__(self,username,password):# 賬號(hào)密碼self.username = usernameself.password = password# 對(duì)密碼進(jìn)行加密self.pwd = base64.b64encode(password.encode("utf-8")).decode("ascii")self.type_phone = {"電信": "@telecom","移動(dòng)": "@cmcc","聯(lián)通": "@unicom",# @telecom表示電信 @cmcc表示移動(dòng) @unicom表示聯(lián)通} ?self.url="http://此處打碼/include/auth_action.php"self.headers = {'User-Agent': '自己電腦的請(qǐng)求頭','Referer': 'http://此處打碼/srun_portal_pc.php?ac_id=2&url=此處打碼',} ?def spider_login(self): ?url = self.urlheaders = self.headers ?query_date = {'action': 'login','username': self.username + self.type_phone["電信"],'password': '{B}' + self.pwd,'ac_id': '2','user_ip': '','nas_ip': '','user_mac': '','save_me': '0','ajax': '1',} ?response = requests.post(url=url,headers=headers,data=query_date).content.decode("utf-8")print(response)print("end")return response ? ? class Content_Wifi(Login_Wifi):def __init__(self,username,password,wifi_name):Login_Wifi.__init__(self,username,password)self.wifi_name = wifi_name ? ?def wifi_connect_status(self):print("start")wifi = pywifi.PyWiFi() ?# 定義接口操作iface = wifi.interfaces()[0] ?# 這里iface就是獲取的wifi接口 ?if iface.status() in [const.IFACE_CONNECTED, const.IFACE_INACTIVE]:print("已連接wifi!")return 1else:print("沒(méi)有連接wifi!") ?return 0 ?def connect_wifi(self):wifi = pywifi.PyWiFi() ?#生成對(duì)象而已,接下來(lái)就能對(duì)他進(jìn)行配置操作了ifaces = wifi.interfaces()[0]print(ifaces.name()) ?# 輸出無(wú)線網(wǎng)卡名稱ifaces.disconnect()time.sleep(3) ?profile = pywifi.Profile() ?# 配置文件profile.ssid = self.wifi_name ?# wifi名稱 ? "XZCIT-WIFI"ifaces.remove_all_network_profiles() ?# 刪除其它配置文件tmp_profile = ifaces.add_network_profile(profile) ?# 加載配置文件ifaces.connect(tmp_profile)time.sleep(5)isok = True ?if ifaces.status() == const.IFACE_CONNECTED:print("連接成功!")else:print("連接失敗") ?time.sleep(1)return isok ? ? ? if __name__ == '__main__':self_date = {"username":"學(xué)號(hào)", ? # 測(cè)試賬號(hào)"password":"123456", ? ? ? ? # 測(cè)試密碼}wifi_content = Content_Wifi(username=self_date['username'],password=self_date['password'],wifi_name="校園WIFI" ? ? ?# 校園WIFI)wifi_content.wifi_connect_status()wifi_content.connect_wifi()wifi_content.spider_login() ?利用模塊 Pyinstaller 生成 腳本.exe文件。附圖
最后就是整個(gè)項(xiàng)目的部署與實(shí)現(xiàn)開(kāi)機(jī)自啟動(dòng)。
將生成的.exe可執(zhí)行文件發(fā)送到桌面快敏捷方式——>win+r調(diào)出運(yùn)行框,并輸入shell:startup打開(kāi)啟動(dòng)——>將快敏捷方式放到其中。
大功告成啦!!!!!!!!!!
注:本文僅作為學(xué)術(shù)交流和技術(shù)分析,所有的敏感信息全部打碼,登錄的賬號(hào)為本人自己的賬號(hào),不涉及任何敏感行為,轉(zhuǎn)載請(qǐng)注明
總結(jié)
以上是生活随笔為你收集整理的python爬虫模拟登陆校园网+连接校园wifi的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 分布式数据库基础:分布式事务相关概念介绍
- 下一篇: 大型分布式存储方案MinIO介绍,看完你