题目链接:https://match.yuanrenxue.cn/match/17
如题考点http2.0,使用抓包工具发包看看⬇️

这就得使用python的httpx库了,使用时如果想使用http2.0协议,参数http2需要传True。
完整代码如下:
import json import time import httpx import requests def get_question_17_result(): """http2.0""" url = "https://match.yuanrenxue.cn/api/question/17" headers = { "sec-ch-ua": "\"Chromium\";v=\"94\", \"Google Chrome\";v=\"94\", \";Not A Brand\";v=\"99\"", "accept": "application/json, text/javascript, */*; q=0.01", "x-requested-with": "XMLHttpRequest", "sec-ch-ua-mobile": "?0", "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36", "sec-ch-ua-platform": "\"Windows\"", "sec-fetch-site": "same-origin", "sec-fetch-mode": "cors", "sec-fetch-dest": "empty", "referer": "https://match.yuanrenxue.cn/match/17", # "accept-encoding": "gzip, deflate, br", "accept-language": "zh-CN,zh;q=0.9", # "cookie": "Hm_lvt_f80b2b389f44bbfb3bfe1704817d44e0=1773998064&sessionid=1p34qxd0wn3sc2nlip8vx6izgffrh0m9&m=a37ca75833ca865d11a7451f18043a14|1774000280348&HMACCOUNT=5CCC201FDBE7CB2F&yuanrenxue_cookie=1774248315|H9iyxX7cyNbIKK3lJpCJQzOvYnqIJjbhyhj3BfcKkEeWIOqhIAmtsCqygVmvm4rOBcvzZHu&Hm_lpvt_f80b2b389f44bbfb3bfe1704817d44e0=1774340511" "cookie": "sessionid=1p34qxd0wn3sc2nlip8vx6izgffrh0m9" } sum_data = 0 for page in range(1, 6): params = { "page": page, "pageSize": "10", "kw": "", } try: # 指定使用http2.0协议,默认是http1 with httpx.Client(http2=True) as client: client.headers.update(headers) if page == 5: headers['user-agent'] = 'yuanrenxue' client.headers.update(headers) r = client.get(url=url, params=params) if r.status_code == 200: data = r.json()['data'] sum_data += sum(data) print(f"翻第{str(page)}页,累计求和:{sum_data}") else: print(f'error:{r.status_code}-----{r.text}') except Exception as e: raise ValueError(e) if __name__ == '__main__': get_question_17_result()
实践出真知~
浙公网安备 33010602011771号