下午闲来无事,顺手一写
主要用到了 Requests 模块实现模拟登陆,提交.有中文说明文档
Beautiful Soup, 很强大的HTML/XML的解析器,没用上.
$Python大法好,不到50行即可搞定$
Python代码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#!/usr/bin/env python3
#coding=utf-8
import requests, json
from bs4 import BeautifulSoup
import os
import os.path
import time
sub_dir = "/home/acm/Desktop/ACM_Code/HDUOJ/" #最后那个斜杠一定要加
uname = input("请输入用户名: ")
upass = input("请输入密码: ")
#获取该目录下所有文件
all_file = os.listdir(sub_dir)
login_url = "http://acm.hdu.edu.cn/userloginex.php?action=login"
sub_url = "http://acm.hdu.edu.cn/submit.php?action=submit"
login_data = {'username': uname, "userpass": upass,
"login":"Sign In"}
#保持会话
cookieJar = requests.cookies.RequestsCookieJar()
session = requests.Session()
#登陆
login_resp = session.request("POST", login_url,
cookies=cookieJar, data=login_data)
for fname in all_file:
proid = int(fname[3:7])
subcode = open(sub_dir+fname).read()
print("正在提交" + str(proid))
submit_data = {"check": 0, "problemid": proid,
"language": 0, "usercode":subcode}
#提交
r = session.request("POST", sub_url, cookies=cookieJar,
data=submit_data)
print("提交完成")
time.sleep(1)