使用 Python (尝试)抢洛谷第 6e6 次评测

· · 个人记录

蒟蒻先吐槽几句:

  1. 洛谷 2018 第 (1 + 1) 次评测是我抢到的。

  2. 洛谷第 (88888 + 2) 名用户是我抢到的。

  3. 洛谷第 (6000000 + 1) 次评测是我抢到的。

  4. 这是啥破运气啊!!

  5. 笔者很沮丧,打算抢第 10000 名用户和第 10000000 次评测。

材料准备

观察提交时发送的请求

  1. 打开 Fiddler。
  2. 去 P1001 提交一个 AC 代码,代码越短越好。
  3. 观察 Fiddler 多出来的记录(共 3 条),分别是:
  4. \texttt{https://www.luogu.org/api/problem/createRecord/P1001}
  5. \texttt{https://www.luogu.org/api/record/submitSubtask/<Your RID>}
  6. \texttt{https://www.luogu.org/recordnew/show/<Your RID>?\_pjax=.lg-content}
  7. 查看 Fiddler 给的 # 信息,发现前两个请求是 post。所以,需要再爬虫中模拟这两次请求,第三次忽略。
  8. 分别查看两次 post 所发送的数据。对于两次 post,可以列出如下字典:
    data1 = {
       'lang': '<第几个语言>',
       'code': '<代码>',
       'enableO2': '0',
       'verify': ''
    }
    data2 = {
       'subtaskId': '0'
    }

编写前提代码

import requests
from bs4 import BeautifulSoup
import time
import winsound

import warnings
warnings.filterwarnings("ignore")

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
type = sys.getfilesystemencoding()

headers = { <...> }
cookies = { <...> }
data1 = { <...> }
data2 = { <...> }

编写核心代码

def submit():
    s_v1 = requests.post(url = 'https://www.luogu.org/api/problem/createRecord/P1001', 
        data = data1, headers = headers, cookies = cookies, verify = False)
    s_rid = s_v1.text[28:35]
    s_v2 = requests.post(url = 'https://www.luogu.org/api/record/submitSubtask/' + s_rid, 
        data = data2, headers = headers, cookies = cookies, verify = False)
    print s_rid, s_v2.text[10:13]
    return int(s_rid)

编写检测进度代码

# 这部分代码写得很丑,不要喷我啊
start = 5999960
while True:
    for i in range(start, 6000005):
        url = 'https://www.luogu.org/record/show?rid=' + str(i)
        try:
            v = requests.get(url = url, headers = headers, cookies = cookies, verify = False)
            soup = BeautifulSoup(v.text, 'lxml')
            res = soup.find('div', {'class': 'lg-toolbar'})
            print res.text.split()[0]
            if res is not None:
                if i >= 5999990:
                    print 'calling'
                    winsound.Beep(3000, 500)
                    for j in range(11):
                        if reg() > 6000000:
                            exit(0)
                start += 3
                time.sleep(0.8) # 应 kkk 要求,sleep 长一些
                break
            else:
                time.sleep(0.8)
                break
        except:
            time.sleep(0.8)
            break

编写检测是否抢到的代码

# 其实这一部分是无意义的,不贴了