GitHub: yzxoi/Automatic-Submitter-for-HUSTOJ
为 HUSTOJ 打造的自动提交机
特性
自动化 - 使用 Selenium with Python 实现自动化交题
同步化 - 支持自动爬取大号提交记录以提交至小号
定制化 - 支持自定义选择题目提交
开始 Tips: 推荐使用 Python 3.10+ 版本构建运行。
安装 Python 及依赖库:1 2 3 4 $ choco install python $ pip install selenium $ pip install requests $ pip install lxml
打开终端,运行:1 $ git clone https://github.com/yzxoi/Automatic-Submitter-for-HUSTOJ.git
修改 main.py 内的配置文件:1 $ vi Automatic-Submitter-for-HUSTOJ/main.py
运行 main.py:
配置
填写 HUSTOJ 网址 URL。
填写主账号提交者 MAIN_SUBMITTER。该账号应含有某一种语言 所有正确 提交记录。
填写提交语言 LANGUAGE。(对应代码表见附录 )
填写子账号(bot 账号) USER_ID。
填写子账号(bot 账号) PASSWORD。
填写主账号 Cookie:替换 <cookie>
。
1 2 3 4 5 6 7 8 9 10 URL = "http://syzoj.hustoj.com/" MAIN_SUBMITTER = "std" LANGUAGE = "6" USER_ID = "spider" PASSWORD = "spider123456" headers = { "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36" , "Cookie" : "<cookie>" }
cookie 查找方法: 登录主账号,打开 F12 开发者管理工具,打开控制台 Console,输入:
其所返回的 PHPSESSID=qwertyuiop
即为 cookie。
注意当运行本程序时要确保主账号处于登录状态。
附录
语言
代码
C
0
C++
1
Java
3
Python
6
PHP
7
C#
9
JavaScript
16
Go
17
SQL
18
Source Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 import seleniumfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.select import Selectfrom selenium import webdriverimport requests, time, refrom lxml import etreeURL = "http://syzoj.hustoj.com/" MAIN_SUBMITTER = "std" LANGUAGE = "6" USER_ID = "spider" PASSWORD = "spider123456" headers = { "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36" , "Cookie" : "<cookie>" } vis = {} def main (): url = URL + "status.php?problem_id=&user_id=" + MAIN_SUBMITTER + "&language=" + LANGUAGE + "&jresult=4" response = requests.get(url=url, headers=headers) text = response.content.decode("utf-8" ) html = etree.HTML(text) links = html.xpath('//*[@id="result-tab"]/tbody/tr/td[1]/b/text()' ) probs = html.xpath('//*[@id="result-tab"]/tbody/tr/td[4]/b/div/a/text()' ) print ("搜索到的提交记录对应题目:" ,end='' ) print (probs) driver = webdriver.Chrome() url = URL + "loginpage.php" driver.get(url) driver.find_element(By.NAME,"user_id" ).send_keys(USER_ID) driver.find_element(By.NAME,"password" ).send_keys(PASSWORD) time.sleep(1 ) driver.find_element(By.NAME,"submit" ).click() for i in probs: vis[i] = 0 cnt = 0 for link in links: if vis[probs[cnt]] == 0 : vis[probs[cnt]] = 1 url = URL + "showsource.php?id=" + link response = requests.get(url=url, headers=headers) text = response.content.decode("utf-8" ) html = etree.HTML(text) code = html.xpath('//pre/text()' )[0 ] url = URL + "submitpage.php?id=" + probs[cnt] driver.get(url) sel = driver.find_element(By.ID,"language" ) Select(sel).select_by_value(LANGUAGE) time.sleep(1 ) t= "" for j in range (0 ,len (code)): if ord (code[j])==10 : t += "\\n" elif ord (code[j])==13 : t+="\\t" elif code[j]=="'" : t+="\\\'" elif code[j]=="/" and code[j+1 ]=='*' : break else : t+=code[j] stri = "editor.setValue('" + str (t) + "')" driver.execute_script(stri) time.sleep(1 ) driver.find_element(By.ID,"Submit" ).click() time.sleep(10 ) cnt = cnt + 1 print ("cur progress: " + str (cnt) + "/" + str (len (links))) if __name__ == '__main__' : main()