分析
在未登录的情况下访问力扣的主页,可以看到有每日一题的链接
审查元素发现,有一个超链接,即为每日一题的链接
要在这个页面找到这个连接,试着搜索‘https://leetcode.cn/problems/’,发现页面仅含有一个
思路
直接访问https://leetcode.cn,得到页面后搜索https://leetcode.cn/problems/,补全剩下的部分即为力扣每日一题的URL
实现(ChatGPT秒了)
prompt:
使用java,访问https://leetcode.cn,页面中有且仅有一个该字符串https://leetcode.cn/problems/,该字符串以一个a标签内href属性存在,例如<a href="https://leetcode.cn/problems/find-elements-in-a-contaminated-binary-tree/" target="_blank" rel="noreferrer" class="mt-[8px] block rounded px-3 py-2 first:mt-0 bg-fill-tertiary dark:bg-fill-tertiary cursor-pointer hover:bg-fill-secondary dark:hover:bg-fill-secondary"><div class="flex items-center"><img src="https://static.leetcode.cn/cn-frontendx-assets/production/_next/static/images/everyday-problem-cbb6325ef6fecf53712264fb80350ef9.svg" alt="每日 1 题" class="h-[14px] w-[14px]"><span class="ml-1 text-xs font-medium" style="color: rgb(27, 163, 255);">每日 1 题</span></div><div class="mt-2 flex items-center justify-between"><h3 class="text-sm text-text-primary dark:text-text-primary">1261. 在受污染的二叉树中查找元素</h3><div class="flex w-[50px] items-center justify-end"><div data-state="closed"><div class="relative flex items-center justify-center h-3.5 w-3.5"><svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" class="absolute left-0 top-0 h-full w-full fill-transparent" style="stroke-width: 12;"><defs><linearGradient x1="1" y1="0" x2="0" y2="0" id="gradientProgressCircleInfo"><stop offset="0%" stop-color="#007AFF"></stop><stop offset="100%" stop-color="#73B6FF"></stop></linearGradient></defs><circle cx="50" cy="50" r="42" class="stroke-[rgba(0,0,0,0.03)]"></circle><circle cx="50" cy="50" r="42" class="hidden stroke-lc-gray-60 dark:stroke-dark-lc-gray-60" style="stroke-linecap: initial; stroke-dasharray: 0, 264; stroke-dashoffset: 66;"></circle></svg></div></div></div></div></a>
其中https://leetcode.cn/problems/find-elements-in-a-contaminated-binary-tree/为我需要的一个字符串,请你使用java完成该要求
我这边使用了Github的copilot
cv一下,修改一下
结果分析
发现控制台并没有输出每日一题的链接,按照ai的逻辑,是获取所有a标签的href属性,打上断点后调试发现没有https://leetcode.cn/problems/
查看请求后的返回的页面源代码,发现每日一题的部分确实没有,连a标签也没有
重构思路
所以大胆推测,肯定是页面还会有js代码和其他获取每日一题链接的请求,获取到之后再插入到页面源代码中
所以接下来不妨去试着寻找一下请求每日一题的请求
再次分析
来到力扣主页重新刷新,在开发工具里找一下有没有请求每日一题的请求
最后确实发现了,在其中一个graphql的请求后,返回了我们想要的每日一题链接
那接下来就简单了,直接发请求就可以了。但是,页面发起了很多的graphql请求,不可能全是请求每日一题的吧,看一下请求体的负载
可以知道,页面发起的请求的url都是一样的,但是不同的请求体负载会返回不同的结果
实现(ChatGPT秒了)
prompt:
向https://leetcode.cn/graphql/发送post请求,携带的请求体为:
{"query":"\n query CalendarTaskSchedule($days: Int!) {\n calendarTaskSchedule(days: $days) {\n contests {\n id\n name\n slug\n progress\n link\n premiumOnly\n }\n dailyQuestions {\n id\n name\n slug\n progress\n link\n premiumOnly\n }\n studyPlans {\n id\n name\n slug\n progress\n link\n premiumOnly\n }\n }\n}\n ","variables":{"days":0},"operationName":"CalendarTaskSchedule"}
返回的数据结构如下:
{
"data": {
"calendarTaskSchedule": {
"contests": [],
"dailyQuestions": [
{
"id": "2864",
"name": "\u6700\u5927\u4e8c\u8fdb\u5236\u5947\u6570",
"slug": "maximum-odd-binary-number",
"progress": 0,
"link": "https://leetcode.cn/problems/maximum-odd-binary-number/",
"premiumOnly": false
}
],
"studyPlans": []
}
}
}要求取出其中的“link”(在我给出的例子中“link”为"https://leetcode.cn/problems/maximum-odd-binary-number/")
使用java完成
不用犹豫,直接cv,开跑运行,直接成功
总结
用带指定的请求体的post请求即可得到带有力扣每日一题链接的一个json