Skip to content

Instantly share code, notes, and snippets.

@breakTBB
Created November 23, 2022 09:20
Show Gist options
  • Save breakTBB/fe8a1ef2d8efc6f3ac20bb45d9091cdc to your computer and use it in GitHub Desktop.
Save breakTBB/fe8a1ef2d8efc6f3ac20bb45d9091cdc to your computer and use it in GitHub Desktop.
crawl test cases from topcoder
import httpx
import os
from bs4 import BeautifulSoup
data = {
'username' : 'prism17',
'password' : '',
'module' : 'Login'
}
problem_url = 'https://community.topcoder.com/stat?c=problem_solution&rm=319909&rd=15820&pm=12924&cr=22714443'
client = httpx.Client(params=data, follow_redirects=True)
res = client.get(url=problem_url)
test_list, answer_list,null_list = [], [], []
if res.status_code == 200:
page = BeautifulSoup(res.content,"lxml")
testcases = page.find_all("td", class_="statText")
ok, turn = False, 0
for case in testcases:
text = case.text
if ok:
[test_list, answer_list, null_list][turn].append(text)
turn = (turn + 1) % 3
if text == 'Success':
ok = True
with open("input.txt", "w") as f:
f.writelines(s + '\n' for s in test_list)
print(f'input saved as {os.path.realpath(f.name)}')
with open("output.txt", "w") as f:
f.writelines(s + '\n' for s in answer_list)
print(f'output saved as {os.path.realpath(f.name)}')
print('fetch test cases has completed')
else:
print('fail to connect topcoder')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment