Python3 BeautifulSoup爬虫 ZOJ自动提交
2017-06-19 10:57
791 查看
import http.cookiejar import logging import time import urllib.parse import urllib.request from bs4 import BeautifulSoup from submitcode import SubmitCode class SubmitZoj(): def __init__(self, userId, passWord): self.userId = userId self.passWord = passWord cj = http.cookiejar.LWPCookieJar() self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) urllib.request.install_opener(self.opener) self.languageMap = {} self.languageMap["Cpp"] = 2 self.languageMap["Java"] = 4 def login(self): params = { "handle": self.userId, "password": self.passWord, } loginUrl = "http://acm.zju.edu.cn/onlinejudge/login.do" postData = urllib.parse.urlencode(params).encode(encoding='UTF8') html = self.opener.open(loginUrl, postData).read() soup = BeautifulSoup(html, "xml") return soup.prettify().find("logout.do") > -1 def submit(self, pid, language, src): params = { "problemId": pid, "languageId": self.languageMap.get(language) , "source": src, } submitUrl = "http://acm.zju.edu.cn/onlinejudge/submit.do" postData = urllib.parse.urlencode(params).encode(encoding='UTF8') self.opener.open(submitUrl, postData) def status(self): statusUrl = "http://acm.zju.edu.cn/onlinejudge/showRuns.do?contestId=1&search=true&firstId=-1&lastId=-1&problemCode=&handle=" + self.userId + "&idStart=&idEnd=" html = urllib.request.urlopen(statusUrl).read() soup = BeautifulSoup(html, "xml") runIds = soup.find_all("td", "runId") runSubmitTimes = soup.find_all("td", "runSubmitTime") runJudgeStatuss = soup.find_all("span")[2:] runProblemIds = soup.find_all("td", "runProblemId") runLanguages = soup.find_all("td", "runLanguage") runTimes = soup.find_all("td", "runTime") runMemorys = soup.find_all("td", "runMemory") n = len(runIds) for i in range(1 , n): print(runIds[i].string , end="\t") print(runSubmitTimes[i].string , end="\t") try: print(runJudgeStatuss[i].string.strip() , end="\t") except: print(runJudgeStatuss[i].contents[1].string , end="\t") print(runProblemIds[i].string , end="\t") print(runLanguages[i].string , end="\t") print(runTimes[i].string , end="\t") print(runMemorys[i].string , end="\n") if __name__ == '__main__': FORMAT = "[ZojAutoSubmit]-----%(message)s------" logging.basicConfig(level=logging.INFO, format=FORMAT) #用户名 , 密码 (明文) zoj = SubmitZoj("用户名", "密码") if zoj.login(): logging.info("login success!") #zoj.submit("1", "Java" , SubmitCode.javaCode) zoj.submit("2", "Cpp" , SubmitCode.cppCode ) logging.info("submit success!") time.sleep(10) zoj.status() logging.info("search status success!")
class SubmitCode():
javaCode = '''
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}
'''
cppCode='''
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <stack>
#include <map>
#include <math.h>
#include <queue>
using namespace std;
int n,maxnum;
char grid[4][4];
int canput(int x,int y){
int i;
for(i=x-1;i>=0;i--){
if(grid[i][y]=='0')
return 0;
if(grid[i][y]=='X')
break;
}
for(i=y-1;i>=0;i--){
if(grid[x][i]=='0')
return 0;
if(grid[x][i]=='X')
break;
}
return 1;
}
void dfs(int k,int num){
int x,y;
if(k==n*n){
if(num>maxnum){
maxnum=num;
return ;
}
}
else{
x=k/n;
y=k%n;
if(grid[x][y]=='.'&&canput(x,y)){
grid[x][y]='0';
dfs(k+1,num+1);
grid[x][y]='.';
}
dfs(k+1,num);
}
}
int main(){
int i,j;
while(cin>>n&&n){
maxnum=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>grid[i][j];
dfs(0,0);
cout<<maxnum<<endl;
}
}
'''
if __name__ == '__main__':
print(SubmitCode.cppCode)
[ZojAutoSubmit]-----login success!------
[ZojAutoSubmit]-----submit success!------
4019122 2017-06-19 10:52:38 Accepted 1002 C++ 0 284
[ZojAutoSubmit]-----search status success!------
4019119 2017-06-19 10:51:13 Accepted 1001 Java 35 558
4019110 2017-06-19 10:48:41 Accepted 1001 Java 26 558
4019105 2017-06-19 10:46:28 Accepted 1001 Java 44 558
4019102 2017-06-19 10:45:01 Compilation Error 1002 C++ 0 0
4019096 2017-06-19 10:43:25 Accepted 1001 Java 26 558
4019060 2017-06-19 10:25:30 Accepted 1001 Java 27 558
4019053 2017-06-19 10:21:30 Accepted 1001 Java 29 338
4019050 2017-06-19 10:19:38 Accepted 1001 Java 26 558
4019033 2017-06-19 10:08:58 Accepted 1001 Java 52 558
4019006 2017-06-19 09:15:12 Accepted 1001 Java 29 338
4019004 2017-06-19 09:13:08 Compilation Error 1002 C++ 0 0
4019003 2017-06-19 09:09:47 Accepted 1002 C++ 0 284
4019001 2017-06-19 09:09:08 Compilation Error 1002 C++ 0 0
4018998 2017-06-19 09:04:16 Wrong Answer 1001 C++ 0 284
相关文章推荐
- Python3 BeautifulSoup爬虫 HDU自动提交
- Python3 BeautifulSoup爬虫 POJ自动提交
- python爬虫自动提交HDU并获取AC状态(p3+request+Beatifulsoup)
- python 自己写爬虫 ----- BeautifulSoup
- python利用beautifulSoup实现爬虫
- python利用beautifulSoup写爬虫
- Python 的 MySQLdb 模块插入数据没有成功与 autocommit(自动提交)的关系
- poj和zoj自动登录,提交并获取评判结果
- Python3.x爬虫教程:爬网页、爬图片、自动登录
- Python小爬虫-自动下载三亿文库文档
- python爬虫自动搜索下载游民今日搞笑图集
- 我的第一个python爬虫程序(从百度贴吧自动下载图片)
- Python实现博客日志自动提交程序
- python爬虫抓取全国pm2.5的空气质量(BeautifulSoup3)
- beautifulsoup,python3中的爬虫匹配神器
- python利用beautifulSoup实现爬虫
- svn 文件批量删除自动提交脚本(python)
- python 自动提交和抓取网页
- python简易爬虫来实现自动图片下载
- python 爬虫 自动登录人人网