跳到正文

个人记录

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

Anguei约 5 分钟
原文
文章信息与版本

文章信息

发布
2018年3月1日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 5 分钟
获赞
3

版本

  1. v1当前

蒟蒻先吐槽几句:

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

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

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

  4. 这是啥破运气啊!!

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

材料准备

  • Chrome: 用于查看结果
  • Fiddler: 用于抓包
  • Visual Studio 2017: 没错,我用 VS 写 Python,它很强
  • 一颗临近开学还勇于不写寒假作业的心

观察提交时发送的请求

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

编写前提代码

Python
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 = { <...> }

编写核心代码

Python
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)

编写检测进度代码

Python
# 这部分代码写得很丑,不要喷我啊
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

编写检测是否抢到的代码

Python
# 其实这一部分是无意义的,不贴了
原文
存档评论12 条抓取于