Python 으로 스마트 캠퍼스 매일 수업 표를 가 져 와 메 일 로 자동 으로 보 냅 니 다.

준비 작업
1.1 로그 인 인터페이스 관찰
image-20210517162309331
나 는 우리 가 계 정과 비밀 번 호 를 입력 한 후에 이런 링크 가 나타 나 는 것 을 쉽게 발견 할 수 있다.Response 가 돌아 온 것 은 false 이다.http://sso.cqcet.edu.cn/verificationCode?userCode={username}이곳 의username은 당신 의 을 말 합 니 다.false로그 인 할 때 인증 코드 를 입력 할 필요 가 없다 는 것 을 나타 낸다.Response 가 되 돌아 오 는 것 이true일 때 인증 코드 를 입력 해 야 합 니 다.
1.2 로그 인 요청 과정 관찰
image-20210517163738181
우 리 는 Request URL 이http://sso.cqcet.edu.cn/uaa/login_process라 는 것 을 발견 할 수 있 습 니 다.
가지 고 있 는 폼 데 이 터 는 다음 과 같 습 니 다.
image-20210517164100758
1.3 방문 시간표 의 url 요청 관찰
시간표 가 있 는 곳 을 찾 아 클릭 하면 Request URL 이http://ossc.cqcet.edu.cn/api/schedule/query인 것 을 발견 할 수 있 습 니 다.너 는 Preview 에서 시간표 정 보 를 발견 할 수 있다.
image-20210517164841771
그러나 이 링크 를 더 블 클릭 하면 다음 과 같은 오류 가 발생 합 니 다.
image-20210517164603506
원인:이 링크 에 접근 하려 면 요청 부하 가 필요 합 니 다.
image-20210517164750907
Date 는 조회 의 구체 적 인 어느 날 의 수업 표를 나타 낸다.
코드 구현
2.1 해당 의존 라 이브 러 리 설치
키보드 누 르 기Win+R다음 화면 으로 이동:
image-20210517165443143
입력cmd,다음 화면 에 입장:
image-20210517165603495
입력pip install 하면 설치 할 수 있 습 니 다.
2.2 의존 라 이브 러 리 가 져 오기

import json
import requests
import uuid #         ,      
import yagmail #       
import random
import datetime #    
from fake_useragent import UserAgent #        UserAgent
2.3 일부 계 정 비밀번호 의 기록

username = '********' #   
passwd = '*********' #   
now_time = datetime.datetime.now ().strftime('%Y-%m-%d') #     
contents = '' #           

#     uuid
uuid = uuid.uuid4()


#   UA
def get_random_ua():
    ua = UserAgent()
    return ua.random
2.4 url 집계 및 기타 준비

# url  
code_url = f'http://sso.cqcet.edu.cn/validata/code/{uuid}'  #      url
url = f'http://sso.cqcet.edu.cn/verificationCode?userCode={username}'  #            url
login_url = 'http://sso.cqcet.edu.cn/uaa/login_process'  #   url
info_url = 'http://ossc.cqcet.edu.cn/getUserInfo'  #       url
schedule_url = 'http://ossc.cqcet.edu.cn/api/schedule/query'  #   url

#     
headers = {
    'User-Agent': get_random_ua()
}

session = requests.session()


#        
def getImgCode():
    img_name = str(uuid) + '.jpg'
    img_data = requests.get(url=code_url, headers=headers).content
    with open(img_name, 'wb') as fp:
        fp.write(img_data)
        print('       ,     !')
2.5 구체 적 과정

#        
def getImgCode():
    img_name = str(uuid) + '.jpg'
    img_data = requests.get(url=code_url, headers=headers).content
    with open(img_name, 'wb') as fp:
        fp.write(img_data)
        print('       ,     !')


#            
isCode = session.get(url=url, headers=headers).text

if isCode == 'true':
    getImgCode()
    #   img_code
    img_code = input('      :')
else:
    img_code = ''

data = {
    'type': '1',
    'deviceId': str(uuid),
    'username': username,
    'password': passwd,
    'img_code': img_code
}
response = session.post(url=login_url, data=data)
login_text = response.text
# print(response.status_code)  #   200         !

#       
info = session.get(url=info_url).text
info_data = json.loads(info)  #  json       
# print(info_data)
# pprint.pprint(info_data)

contents += '   ' + info_data['user']['name'] + '  :
' # schedule_json = { 'endDate': now_time, 'limit': '10', 'page': '1', 'startDate': now_time, 'type': '0' } schedule = session.post(url=schedule_url, json=schedule_json).text schedule_data = json.loads(schedule) # json # pprint.pprint(len(schedule_data['data'])) # # if len(schedule_data['data']) == 0: re_list = [' , ~', ' , !', ' , ~
?', ' , ~'] contents += '        ' + re_list[random.randint(0, 3)] else: contents += '         ~
<hr/><table border="1" cellpadding="2" ' \ 'cellspacing="0"><tr><th> </th><th> </th><th> </th><th> </th></tr>' for course_table in schedule_data['data']: # print(course_table) time = '<tr><td>' + course_table['date'] + ' ' + course_table['time'] + '</td>' title = '<td>' + course_table['activity']['title'] + '</td>' teacher = '<td>' + course_table['activity']['remarks'] + '</td>' classroom = '<td>' + course_table['activity']['address'] + '</td></tr>' contents += time + title + teacher + classroom contents += '</table>'
2.6 얻 은 수업 시간표 정 보 를 우편 으로 자신 에 게 보낸다.

#       
def to_mail(content):
    yag = yagmail.SMTP(
        user='sm*****[email protected]',
        host='smtp.qq.com',
        password="pdxj**********", #                ,     。       yagmail   。
        smtp_ssl=True)
    yag.send(to=['[email protected]','s*****[email protected]'],
             subject='  ,      ~',
             contents=contents)
             
to_mail(contents)  #         
3.효과 전시
在这里插入图片描述
파 이 썬 으로 스마트 캠퍼스 일 일 수업 표를 받 아 자동 으로 메 일 로 보 내 는 글 을 소개 합 니 다.더 많은 파 이 썬 이 수업 표를 받 은 후 메 일 로 보 내 는 내용 은 예전 의 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 도 많은 응원 부 탁 드 리 겠 습 니 다!

좋은 웹페이지 즐겨찾기