Skip to content

Instantly share code, notes, and snippets.

@iydon
Created February 15, 2022 05:43
Show Gist options
  • Save iydon/83ae206e5391c03731ec4235793b6ed7 to your computer and use it in GitHub Desktop.
Save iydon/83ae206e5391c03731ec4235793b6ed7 to your computer and use it in GitHub Desktop.
年代已久的电费余量查询
import datetime
import requests
from bs4 import BeautifulSoup
class SUSTech:
_url_login = 'https://cas.sustech.edu.cn/cas/login'
def __init__(self, username, password):
self._cookies = self._login(username, password)
def dfyl(self, ldid, mph):
'''电费余量
Argument:
- ldid: [str, int], 楼栋
- mph: [str, int], 门牌号
Note:
- '%Y-%m-%d %H:%M:%S'
'''
url = 'http://ehall.sustech.edu.cn/dxggyw/sys/sdjfgl/paymentController/getFjyl.do'
params = {
'ldid': ldid, 'mph': mph,
}
response = requests.get(url, params=params, cookies=self._cookies)
data = response.json()['data']['data'][0]
date = datetime.datetime.strptime(data['clrq'], '%Y-%m-%d %H:%M:%S')
return date, data['dfyl']
def _login(self, username, password):
data = {
'username': username, 'password': password,
'execution': self._execution(),
'_eventId': 'submit', 'geolocation': '',
}
response = requests.post(self._url_login, data=data)
return dict(response.cookies)
def _execution(self):
soup = BeautifulSoup(requests.get(self._url_login).content, 'lxml')
for item in soup.find_all('input'):
if item.attrs['name'] == 'execution':
return item.attrs['value']
return ''
if __name__ == '__main__':
import pathlib
directory = pathlib.Path('data')
directory.mkdir(exist_ok=True)
# 如果不用分享脚本,将账号密码硬编码即可
date, value = SUSTech(input('账号:'), input('密码:')).dfyl(195, 209)
print(date, ':', value)
path = directory / str(int(date.timestamp()))
if not path.exists():
path.write_text(str(value))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment