本文主要是介绍Python3 BeautifulSoup爬虫 POJ自动提交,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
POJ 提交代码采用Base64加密方式
import http.cookiejar
import logging
import urllib.parse
import urllib.request
import base64
from bs4 import BeautifulSoup
from submitcode import SubmitCodeclass SubmitPoj():def __init__(self, userId, passWord):self.userId = userIdself.passWord = passWordcj = http.cookiejar.LWPCookieJar()self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))urllib.request.install_opener(self.opener)def decode(self , code):bytesString = code.encode(encoding="utf-8")encodestr = base64.b64encode(bytesString)return encodestrdef login(self):params = {"user_id1": self.userId,"password1": self.passWord,"B1": "login","url": "/"}loginUrl = "http://poj.org/login"postData = urllib.parse.urlencode(params).encode(encoding='UTF8')html = self.opener.open(loginUrl, postData).read()soup = BeautifulSoup(html, "xml")return soup.prettify().find("login?action=logout") > -1def submit(self, pid, language, src):params = {"problem_id": pid,"language": language,"source": src,"submit": "Submit","encoded": 1}submitUrl = "http://poj.org/submit"postData = urllib.parse.urlencode(params).encode(encoding='UTF8')self.opener.open(submitUrl, postData)def status(self):statusUrl = "http://poj.org/status?problem_id=&user_id=" + self.userId + "&result=&language="html = urllib.request.urlopen(statusUrl).read()soup = BeautifulSoup(html, "xml")trs = soup.find_all("tr")for row in trs[5:]:contest = row.contentsprint(contest[1].string, end="\t")print(contest[3].contents[0].string.split(">")[1], end="\t")try:print(contest[4].contents[0].contents[1].string.split(">")[1], end="\t")except:print(contest[4].contents[0].string.split(">")[1], end="\t")print(contest[7].contents[0].string, end="\t")print(contest[8].contents[0].string, end="\t")print(contest[9].contents[0].string, end="\t\n")if __name__ == '__main__':FORMAT = "[PojAutoSubmit]-----%(message)s------"logging.basicConfig(level=logging.INFO, format=FORMAT)#用户名 , 密码 (明文)poj = SubmitPoj("用户名", "密码")if poj.login():logging.info("login success!")poj.submit("1700", 2 , poj.decode(SubmitCode.javaCode))poj.submit("1000", 0 , poj.decode(SubmitCode.cppCode))logging.info("submit success!")poj.status()logging.info("search status success!")
class SubmitCode():javaCode = '''
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer; public class Main { public static void main(String[] args) { new Task().solve() ; }
} class Task{ InputReader in = new InputReader(System.in) ; PrintWriter out = new PrintWriter(System.out) ; long sum(int left , int right , int[] num){ if(left == right || right == left + 1) return num[right] ; else if(right == left + 2) return num[left] + num[left+1] + num[left+2] ; else{ long way1 = (num[right] + num[left] + num[right-1] + num[left]) ; //(最慢最快)->最快回->(次慢最快)->最快回 long way2 = (num[left+1] + num[left+1] + num[right] + num[left]) ; //(次快最快)->次快回->(最慢次慢)->最快回 return Math.min(way1, way2) + sum(left , right-2 , num) ; } } void solve(){ int t = in.nextInt() ; while(t-- > 0){ int n = in.nextInt() ; int[] num = new int[n] ; for(int i = 0 ; i < n ; i++) num[i] = in.nextInt() ; Arrays.sort(num) ; out.println(sum(0, n-1 , num)) ; } out.flush() ; } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = new StringTokenizer(""); } private void eat(String s) { tokenizer = new StringTokenizer(s); } public String nextLine() { try { return reader.readLine(); } catch (Exception e) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } } '''cppCode='''
#include <iostream>using namespace std;int main()
{int a,b;cin >> a >> b;cout << a+b << endl;return 0;
}'''if __name__ == '__main__':print(SubmitCode.javaCode)
17090613 1700 Accepted Java 2823B 2017-06-19 08:40:34
17090612 1000 Accepted G++ 134B 2017-06-19 08:36:37
17086082 1000 Compile Error G++ 47B 2017-06-16 17:15:03
17086081 1000 Accepted G++ 128B 2017-06-16 17:14:51
17086072 1000 Compile Error Java 128B 2017-06-16 17:10:43
17086062 1000 Compile Error G++ 47B 2017-06-16 17:07:19
17085929 1030 Compile Error GCC 34B 2017-06-16 15:51:16
17085926 1010 Compile Error GCC 34B 2017-06-16 15:49:54
17085905 1000 Compile Error GCC 34B 2017-06-16 15:45:17
17085888 1001 Compile Error GCC 34B 2017-06-16 15:41:43
17085882 1000 Compile Error Java 640B 2017-06-16 15:39:36
17085878 1000 Accepted Java 479B 2017-06-16 15:38:29
17085781 1000 Compile Error Java 420B 2017-06-16 14:52:23
17085780 1000 Compile Error Java 560B 2017-06-16 14:52:17
17085773 1000 Accepted Java 313B 2017-06-16 14:48:29
17085766 1000 Accepted Java 313B 2017-06-16 14:46:57
16839463 3237 Accepted Java 5998B 2017-04-06 14:11:27
16838503 2763 Accepted Java 5510B 2017-04-06 09:12:45
16835470 2763 Accepted Java 5376B 2017-04-05 17:13:41
16834457 3321 Accepted Java 2645B 2017-04-05 14:02:26
这篇关于Python3 BeautifulSoup爬虫 POJ自动提交的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!