Original Repo: ISKalsi/Leetscrape
An unofficial utility that can download Leetcode problems using its GraphQL API.
leetscrape --daily-challenge questionleetscrape --name "two sum" questionleetscrape --number 58 solution --lang C++leetscrape --url "leetcode.com/problems/3sum" solution --lang javaMore details in the Usage section.
-
-
Make sure you have Go (
1.21or greater) installed with your$GOROOTenvironment variable correctly setup. (If you installed Go in the default location, then you don't need to worry about setting up$GOROOT) -
Run the following command in your shell
go install github.com/vldcreation/leet-scrape/v2/cmd/leetscrape@latest
-
For every problem you can either download its default code snippet or the question statement. This is done by using the following sub-commands -
questionorques- This downloads the full question (including images) in an HTML filesolutionorsol- This downloads the default code snippet in the Leetcode's editor when you start solving a fresh problem. You have to specify the language using the--lang(or-l) flag (choose any one of the available options given in Leetcode's editor)
This tool provides four ways to find a problem -
| Searching Method | Flag | Example |
|---|---|---|
| Name | --name or -n |
leetscrape -n "two sum" sol python |
| Number | --number or -N |
leetscrape -N 1 sol -l C++ |
| URL | --url or -u |
leetscrape -u "https://leetcode.com/problems/two-sum/" ques |
| Daily Challenge | --daily-challenge or -d |
leetscrape -d ques |
Note: providing one of the four methods is necessary.
--output-diror-oCommand:leetscrapeExample:leetscrape -o random/path -n "two sum" quesBy default the file is downloaded to the working directory. To provide a custom path, specify the flag and give a ** relative** path.--boilerplateor-bCommand:solutionExample:leetscrape -n "two sum" sol -l C++ -b "#include<string>\nusing namespace std;\n"This will prepend the given string to the code snippet before writing to file.--templateor-tCommand:solutionExample:leetscrape -n "two sum" sol -l go -t "easy" or "path_to_your_.tmpl"This will use the given string as a template to generate the file name. The template should follow tmpl formatting and contain {{.CodeSnippet}}
-
Two Reasons -
First: I solve my Leetcode problems in a local IDE instead of their embedded editor because it's so shitty for non-premium users (no formatting/autocomplete/debugging). So this is what used to happen - Go to the problem, copy the code in editor, make new file, paste, add #include headers and other stuff. I had to repeat all those things for every new problem. Now I can type a simple command get it all done in one go. Second: Well, just for fun :)
-
TL;DR - Luck + Hit and trial.
I originally planned to make this tool using web-scrapping (via Colly). I struggled quite a lot in between the process when I needed to scrape the page after entering a search query in the Problemset page of Leetcode. The problem was that Colly doesn't support scrapping in an AJAX loaded site (meaning - the page first loads all the question from 1-50, then executes the search query and displays results. Colly scrapes and stops before the query can even happen). To solve this I would have to use a Selenium like package which would involve chromium driven browser. Didn't wanna do that.
While I was searching for some solution in the Inspect-Element window, I found a few graphql entries in the network tab. Had no clue what GraphQL was at that time (it is really cool thing, must check it out). Randomly opened a few of them and there I found what I really needed but wasn't looking for. Then I spent a few hours getting to know how GraphQL works and reverse-engineered the queries from the network tab, and it worked!
-
No, as long as you enter the correct spellings, it will be alright. Check this out for a few examples of a valid names.
-
Again, no. Just make sure it is in double quotes to avoid any unexpected errors. Check this out for a few examples of a valid urls.

