练习平台 第一题代码
import time
import base64
import hashlib
import requests
headers = {
'authority': 'www.python-spider.com',
'accept': 'application/json, text/javascript, */*; q=0.01',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'no-cache',
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'origin': 'https://www.python-spider.com',
'pragma': 'no-cache',
'referer': 'https://www.python-spider.com/challenge/1',
'sec-ch-ua': '"Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
'x-requested-with': 'XMLHttpRequest'
}
url = 'https://www.python-spider.com/api/challenge1'
sun_num = 0
for page in range(1, 101):
form_data = {
'page': str(page),
}
a = '9622'
timestamp, *_ = str(time.time()).split('.')
jm_var = a + timestamp
var_timp = str(base64.b64encode(jm_var.encode('utf-8')), 'utf-8')
md = hashlib.md5(var_timp.encode())
tokens = md.hexdigest()
headers['timestamp'] = timestamp
headers['safe'] = tokens
res = requests.post(headers=headers, url=url, data=form_data).json()
state = res.get('state')
if state == 'success':
datas = res.get('data')
for data in datas:
value = int(data.get('value'))
print(value)
sun_num += value
print('\033[3;31m第{}页面:{}\033[0m\n'.format(page, sun_num))
print('total:{}'.format(sun_num))