Python是一門很受歡迎的編程語言,它有著廣泛的應(yīng)用和便捷的編寫方式,因此也被用來編寫很多教務(wù)搶課的程序。
import requests import time def select_course(course_code): s = requests.session() login_url = 'http://xxx/login' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36'} login_form_data = {'username': 'xxxx', 'password': 'xxxx'} s.post(login_url, data=login_form_data, headers=headers) course_url = 'http://xxx/select_course' course_form_data = {'course_code': course_code} while True: try: response = s.post(course_url, data=course_form_data, headers=headers, timeout=0.1) if '選課成功' in response.text: print('選課成功') break else: print('未選上') except requests.Timeout: pass except Exception as e: print(e) if __name__ == '__main__': course_code = 'xxxxx' select_course(course_code)
我們可以通過requests庫模擬登錄教務(wù)網(wǎng)站,然后不斷地向選課接口發(fā)送請求,直到選課成功或者超時,這樣就可以輕松搶到心儀的課程了。