LeetCode Solution
Parse a single LeetCode community solution post into structured JSON with its author, tags, write-up, extracted code blocks, and engagement counts. To try it with up to 5,000 free requests, start from the LeetCode Scraper API page.
Many posts advertise several languages but put the alternatives behind tabs that render one at a time. codeBlocks holds the blocks present in the write-up as served, so a post titled for three languages can come back with blocks in one.
API usage
Add &scraper=leetcode-solution to a Crawling API request. URL-encode the target URL in the url parameter.
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)Example input URL
Any LeetCode community solution post works in the url parameter - the page at /problems/<slug>/solutions/<id>/<post-slug>/. For example:
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/Response shape
JSON response body. Field types may be null when the source page omits the value.
cpp.Sample response
{
"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..."
}
]
}