|
| 1 | +# LeetCode Scraper |
| 2 | + |
| 3 | +A Python tool to scrape problem details from LeetCode and save them in JSON format. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Scrape LeetCode problems by slug (URL name) |
| 8 | +- Extract problem title, description, examples, and constraints |
| 9 | +- Extract hints, follow-ups, and solutions when available |
| 10 | +- Save data in structured JSON format |
| 11 | +- Get a list of available problems with filtering options |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +1. Clone this repository |
| 16 | +2. Install the required dependencies: |
| 17 | + |
| 18 | +```bash |
| 19 | +pip install -r requirements.txt |
| 20 | +``` |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +### Scrape a Specific Problem |
| 25 | + |
| 26 | +```python |
| 27 | +from leetcode_scraper import LeetCodeScraper |
| 28 | + |
| 29 | +scraper = LeetCodeScraper() |
| 30 | +problem_data = scraper.scrape_problem("two-sum") |
| 31 | +print(problem_data) |
| 32 | +``` |
| 33 | + |
| 34 | +### Scrape Multiple Problems |
| 35 | + |
| 36 | +```python |
| 37 | +scraper = LeetCodeScraper() |
| 38 | +problem_list = scraper.scrape_problem_list(limit=5) # Get 5 problems |
| 39 | + |
| 40 | +for problem in problem_list: |
| 41 | + print(f"Scraping: {problem['title']}") |
| 42 | + scraper.scrape_problem(problem['slug']) |
| 43 | + time.sleep(2) # Add delay between requests |
| 44 | +``` |
| 45 | + |
| 46 | +## Output Format |
| 47 | + |
| 48 | +The scraper saves each problem as a JSON file with the following structure: |
| 49 | + |
| 50 | +```json |
| 51 | +{ |
| 52 | + "title": "Two Sum", |
| 53 | + "problem_id": "1", |
| 54 | + "frontend_id": "1", |
| 55 | + "difficulty": "Easy", |
| 56 | + "problem_slug": "two-sum", |
| 57 | + "topics": ["Array", "Hash Table"], |
| 58 | + "description": "Given an array of integers nums and an integer target...", |
| 59 | + "examples": [ |
| 60 | + { |
| 61 | + "example_num": 1, |
| 62 | + "example_text": "Input: nums = [2,7,11,15], target = 9\nOutput: [0,1]" |
| 63 | + } |
| 64 | + ], |
| 65 | + "constraints": [ |
| 66 | + "2 <= nums.length <= 10^4", |
| 67 | + "-10^9 <= nums[i] <= 10^9", |
| 68 | + "-10^9 <= target <= 10^9" |
| 69 | + ], |
| 70 | + "follow_ups": [ |
| 71 | + "Follow-up: Can you come up with an algorithm that is less than O(n²) time complexity?" |
| 72 | + ], |
| 73 | + "hints": [ |
| 74 | + "A really brute force way would be to search for all possible pairs of numbers but that would be too slow.", |
| 75 | + "Try to use the fact that the array is sorted and use two pointers to speed up the search." |
| 76 | + ], |
| 77 | + "code_snippets": { |
| 78 | + "python": "class Solution:\n def twoSum(self, nums: List[int], target: int) -> List[int]:\n " |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +## Notes |
| 84 | + |
| 85 | +- Be respectful of LeetCode's servers and avoid making too many requests in a short period |
| 86 | +- The tool adds a delay between requests to avoid being rate-limited |
0 commit comments