LeetCode Solution
将单篇 LeetCode 社区题解帖子解析为结构化 JSON,包含作者、标签、正文、提取出的代码块和互动数据。
仅返回已渲染的代码块
很多帖子在标题中标注了多种语言,但把其余语言放在一次只渲染一个的标签页后面。codeBlocks 只包含所返回正文中实际存在的代码块,因此标题标注三种语言的帖子可能只返回其中一种语言的代码块。
API 用法
在 Crawling API 请求中加上 &scraper=leetcode-solution。目标 URL 需经过 URL 编码后放入 url 参数。
curl 'https://api.crawlbase.com/?token=YOUR_TOKEN' \
--data-urlencode 'url=https://leetcode.com/problems/two-sum/solutions/3619262/3-methods-c-java-python-beginner-friendl-x595/' \
--data-urlencode 'scraper=leetcode-solution' -Gfrom crawlbase import CrawlingAPI
api = CrawlingAPI({'token': 'YOUR_TOKEN'})
res = api.get(
'https://leetcode.com/problems/two-sum/solutions/3619262/3-methods-c-java-python-beginner-friendl-x595/',
{'scraper': 'leetcode-solution'}
)
import json
data = json.loads(res['body'])const { CrawlingAPI } = require('crawlbase');
const api = new CrawlingAPI({ token: 'YOUR_TOKEN' });
const res = await api.get(
'https://leetcode.com/problems/two-sum/solutions/3619262/3-methods-c-java-python-beginner-friendl-x595/',
{ scraper: 'leetcode-solution' }
);
const data = JSON.parse(res.body);require 'crawlbase'
api = Crawlbase::API.new(token: 'YOUR_TOKEN')
res = api.get('https://leetcode.com/problems/two-sum/solutions/3619262/3-methods-c-java-python-beginner-friendl-x595/', scraper: 'leetcode-solution')
data = JSON.parse(res.body)示例输入 URL
url 参数支持任意 LeetCode 社区题解帖子 - 即 /problems/<slug>/solutions/<id>/<post-slug>/ 的页面。例如:
https://leetcode.com/problems/two-sum/solutions/3619262/3-methods-c-java-python-beginner-friendl-x595/
https://leetcode.com/problems/two-sum/solutions/127810/two-sum-by-leetcode-kwuq/响应结构
JSON 响应体。当源页面省略对应值时,字段类型可能为 null。
被抓取的题解帖子的 URL。
从帖子 URL 中取得的帖子标识符,以字符串返回。
该帖子所属题目的 slug。
题目页面的绝对 URL。
帖子标题。
发布该帖子的账号。
作者显示名称。
作者的主页用户名。
作者主页的绝对 URL。
帖子上显示的发布日期。
帖子上的标签 - 主题与语言 - 以字符串数组返回。
帖子的浏览数。
帖子获得的点赞数。
帖子的评论数。
展开为纯文本的帖子正文。
HTML 形式的帖子正文,保留标题、列表和代码标记。
从正文中提取的代码块,按文档顺序排列。
LeetCode 标注的代码块语言,例如
cpp。代码块的源码,保留原有缩进。
示例响应
{
"url": "https://leetcode.com/problems/two-sum/solutions/3619262/3-methods-c-java-python-beginner-friendl-x595/",
"id": "3619262",
"problem": "two-sum",
"problemUrl": "https://leetcode.com/problems/two-sum/",
"title": "✅3 Method's || C++ || JAVA || PYTHON || Beginner Friendly🔥🔥🔥",
"author": {
"name": "Rahul Varma",
"username": "rahulvarma5297",
"url": "https://leetcode.com/u/rahulvarma5297/"
},
"postedAt": "Jun 09, 2023",
"tags": [
"Array",
"Hash Table",
"C++",
"Java"
],
"views": 2368905,
"upvotes": 12000,
"comments": 285,
"content": "Intuition\n\nThe Two Sum problem asks us to find two numbers in an array that sum up to a given target value. We need to r...",
"contentHtml": "Intuition
\n\nThe Two Sum problem asks us to find two numbers in an array that sum up to a given...",
"codeBlocks": [
{
"language": "cpp",
"code": "class Solution {\npublic:\n vector twoSum(vector& nums, int target) {\n int n = nums.size();\n..."
}
]
}