crawlbase文档
登录
会员题目仅返回元数据

isPremiumtrue 的题目会保留其标识字段 - id、标题、难度、主题标签 - 但题目描述和互动计数并不在页面中,因此 descriptiondescriptionHtml 及各项计数会返回空值。

API 用法

Crawling API 请求中加上 &scraper=leetcode-problem。目标 URL 需经过 URL 编码后放入 url 参数。

curl 'https://api.crawlbase.com/?token=YOUR_TOKEN' \
  --data-urlencode 'url=https://leetcode.com/problems/two-sum/' \
  --data-urlencode 'scraper=leetcode-problem' -G
from crawlbase import CrawlingAPI

api = CrawlingAPI({'token': 'YOUR_TOKEN'})
res = api.get(
    'https://leetcode.com/problems/two-sum/',
    {'scraper': 'leetcode-problem'}
)

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/',
  { scraper: 'leetcode-problem' }
);
const data = JSON.parse(res.body);
require 'crawlbase'
api = Crawlbase::API.new(token: 'YOUR_TOKEN')

res = api.get('https://leetcode.com/problems/two-sum/', scraper: 'leetcode-problem')
data = JSON.parse(res.body)

示例输入 URL

url 参数支持任意 LeetCode 题目页面 - 即 /problems/<slug> 的页面。例如:

https://leetcode.com/problems/two-sum/
https://leetcode.com/problems/add-two-numbers/
https://leetcode.com/problems/longest-substring-without-repeating-characters/

响应结构

JSON 响应体。当源页面省略对应值时,字段类型可能为 null

url
string
被抓取的题目页面的 URL。
id
string | null
页面上显示的题号,以字符串返回。
internalId
string | null
LeetCode 内部的题目标识符。通常与 id 相同,但在较早的题目上两者会不一致。
title
string | null
题目显示标题。
titleSlug
string | null
题目的 URL slug。
difficulty
string | null
难度标签 - EasyMediumHard
category
string | null
题目分类,例如 AlgorithmsDatabase
isPremium
boolean
该题目是否需要 LeetCode 订阅才能访问。
descriptionHtml
string | null
HTML 形式的题目描述,保留原始标记,使示例、约束条件和行内代码完整无损。
description
string | null
展开为纯文本的题目描述。
likes
integer | null
题目获得的点赞数。
dislikes
integer | null
题目获得的点踩数。
acceptedCount
integer | null
通过的提交次数。
submissionCount
integer | null
提交总次数。
acceptanceRate
string | null
页面上显示的通过率,含百分号。
commentCount
integer | null
题目讨论区中的评论数。
topicTags
array
题目所标注的算法与数据结构标签。
topicTags[].name
string | null
标签显示名称。
topicTags[].slug
string | null
标签的 URL slug。
hints
array
随题目发布的提示,以纯文本字符串数组返回。
exampleTestcases
array
默认测试用例输入,以字符串数组返回。每个字符串保留用于分隔参数的换行符。
codeSnippets
array
LeetCode 为该题目提供的初始代码模板。
codeSnippets[].language
string | null
语言显示名称,例如 Python3
codeSnippets[].languageSlug
string | null
语言 slug,例如 python3
codeSnippets[].code
string | null
模板本身,保留原有缩进。
similarQuestions
array
LeetCode 关联为相关题目的条目。
similarQuestions[].title
string | null
相关题目的标题。
similarQuestions[].titleSlug
string | null
相关题目的 URL slug。
similarQuestions[].url
string | null
相关题目的绝对 URL。
similarQuestions[].difficulty
string | null
相关题目的难度标签。
similarQuestions[].isPremium
boolean
相关题目是否需要订阅才能访问。
officialSolution
object
LeetCode 官方题解的状态。
officialSolution.available
boolean
是否存在官方题解。
officialSolution.isFree
boolean
官方题解是否无需订阅即可阅读。
officialSolution.hasVideo
boolean
官方题解是否包含视频讲解。
solutionsUrl
string | null
该题目社区题解标签页的绝对 URL。

示例响应

{
  "url": "https://leetcode.com/problems/two-sum/",
  "id": "1",
  "internalId": "1",
  "title": "Two Sum",
  "titleSlug": "two-sum",
  "difficulty": "Easy",
  "category": "Algorithms",
  "isPremium": false,
  "descriptionHtml": "

You are given an array of integers nums and an integer target, return indices of t...", "description": "You are given an array of integers nums and an integer target, return indices of the two numbers such that they add up t...", "likes": 69478, "dislikes": 2592, "acceptedCount": 22929319, "submissionCount": 39569455, "acceptanceRate": "57.9%", "commentCount": 3490, "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Hash Table", "slug": "hash-table" } ], "hints": [ "A really brute force way would be to search for all possible pairs of numbers but that would be too slow. Again, it's best to try out brute force solutions just for completeness. It is from these brute force solutions that you can come up with optimizations." ], "exampleTestcases": [ "[2,7,11,15]\n9", "[3,2,4]\n6", "[3,3]\n6" ], "codeSnippets": [ { "language": "Python3", "languageSlug": "python3", "code": "class Solution:\n def twoSum(self, nums: List[int], target: int) -> List[int]:\n " }, { "language": "JavaScript", "languageSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @param {number} target\n * @return {number[]}\n */\nvar twoSum = function(nums, target) {\n \n};" } ], "similarQuestions": [ { "title": "3Sum", "titleSlug": "3sum", "url": "https://leetcode.com/problems/3sum/", "difficulty": "Medium", "isPremium": false }, { "title": "4Sum", "titleSlug": "4sum", "url": "https://leetcode.com/problems/4sum/", "difficulty": "Medium", "isPremium": false } ], "officialSolution": { "available": true, "isFree": true, "hasVideo": true }, "solutionsUrl": "https://leetcode.com/problems/two-sum/solutions/" }