crawlbase文档
登录
仅返回已渲染的代码块

很多帖子在标题中标注了多种语言,但把其余语言放在一次只渲染一个的标签页后面。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' -G
from 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
string
被抓取的题解帖子的 URL。
id
string | null
从帖子 URL 中取得的帖子标识符,以字符串返回。
problem
string | null
该帖子所属题目的 slug。
problemUrl
string | null
题目页面的绝对 URL。
title
string | null
帖子标题。
author
object
发布该帖子的账号。
author.name
string | null
作者显示名称。
author.username
string | null
作者的主页用户名。
author.url
string | null
作者主页的绝对 URL。
postedAt
string | null
帖子上显示的发布日期。
tags
array
帖子上的标签 - 主题与语言 - 以字符串数组返回。
views
integer | null
帖子的浏览数。
upvotes
integer | null
帖子获得的点赞数。
comments
integer | null
帖子的评论数。
content
string | null
展开为纯文本的帖子正文。
contentHtml
string | null
HTML 形式的帖子正文,保留标题、列表和代码标记。
codeBlocks
array
从正文中提取的代码块,按文档顺序排列。
codeBlocks[].language
string | null
LeetCode 标注的代码块语言,例如 cpp
codeBlocks[].code
string | null
代码块的源码,保留原有缩进。

示例响应

{
  "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\n

The 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..." } ] }