LeetCode Problem
Разбирайте страницу отдельной задачи LeetCode в структурированный JSON с условием, сложностью, тематическими тегами, подсказками, заготовками кода и счётчиками вовлечённости.
Задача, у которой isPremium равно true, сохраняет идентифицирующие поля - id, название, сложность, тематические теги, - но условие и счётчики вовлечённости на странице отсутствуют, поэтому description, descriptionHtml и счётчики возвращаются пустыми.
Использование API
Добавьте &scraper=leetcode-problem к запросу Crawling API. Целевой 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' -Gfrom 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, если исходная страница не содержит значения.
id, но у старых задач эти значения расходятся.Easy, Medium или Hard.Algorithms или Database.Python3.python3.Пример ответа
{
"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/"
}