我编写了以下脚本来提交文件here 。 登录工作正常。我不能再多次提交一个问题,但在第一次提交时它可以正常工作。
import mechanize
import cookielib
from bs4 import BeautifulSoup
import html2text
import urllib2
br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
br.addheaders = [('User-agent', 'Chrome')]
# Logging in
br.open('http://codeforces.com/enter')
br.select_form(nr=1)
br.form['handle'] = username
br.form['password'] = password
br.submit()
# Submitting
br.open('http://codeforces.com/problemset/submit')
br.select_form(nr=1)
br.form['submittedProblemCode'] = problemCode
#selecting language
br.form['programTypeId'] = ['42']
br.form.add_file(open("code.cpp"), 'text/plain', "code.cpp")
br.submit()
print br.geturl()
要成功提交,br.geturl()
打印
http://codeforces.com/problemset/status
这是必需的页面,但对于不成功的提交,它会打印
http://codeforces.com/problemset/submit?csrf_token=/insert token/