Package: leetscrape
LeetScrapeIntroducing the LeetScrape - a powerful and efficient Python package designed to scrape problem statements and basic test cases from LeetCode.com. With this package, you can easily download and save LeetCode problems to your local machine, making it convenient for offline practice and studying. It is perfect for software engineers and students preparing for coding interviews. The package is lightweight, easy to use and can be integrated with other tools and IDEs. With the LeetScrape, you can boost your coding skills and improve your chances of landing your dream job.Use this package to get the list of Leetcode questions, their topic and company tags, difficulty, question body (including test cases, constraints, hints), and code stubs in any of the available programming languages.Detailed documentation availablehere.There is also a related Next.js web app to serve the scraped questions and your answers atleetcode-nextjs. See thedemo.Get StartedInstallationStart by installing the package from pip or conda:# Using pippipinstallleetscrape# Using conda:condainstallleetscrape# Using poetry:poetryaddleetscrapeUsageCommand Lineleetscrapelist[--out OUT]- List all questions without generating code stub.options:
-h, --help         show a help message and exit
--out OUT, -o OUT  Specify the output file name to store the list of questions.leetscrapequestion[--out OUT] qid [qid ...]- Generate a code stub for the given question(s).positional arguments:
qid                Enter Leetcode question ID(s)

options:
-h, --help         show this help message and exit
--out OUT, -o OUT  Enter the path to the output directoryleetscrapesolution[-h] [--out OUT] input- Generate mdx files from solutions.positional arguments:
input              Enter the path to the solution directory with solution files or to a single
                    solution file

options:
-h, --help         show this help message and exit
--out OUT, -o OUT  Enter the path to the output directory to save solutions mdx filesleetscrapets[--out OUT]- Create the leetscrape-ts Next.js project to host the solutions.options:
-h, --help         show this help message and exit
--out OUT, -o OUT  Enter the path to the output directory to save the projectPython APIGet the list of problems and their informationfromleetscrapeimportGetQuestionsListls=GetQuestionsList()ls.scrape()# Scrape the list of questionsls.questions.head()# Get the list of questionsQIDtitletitleSlugdifficultyacceptanceRatepaidOnlytopicTagscategorySlug01Two Sumtwo-sumEasy51.4225Falsearray,hash-tablealgorithms12Add Two Numbersadd-two-numbersMedium41.9051Falselinked-list,math,recursionalgorithms23Longest Substring Without Repeating Characterslongest-substring-without-repeating-charactersMedium34.3169Falsehash-table,string,sliding-windowalgorithms34Median of Two Sorted Arraysmedian-of-two-sorted-arraysHard38.8566Falsearray,binary-search,divide-and-conqueralgorithms45Longest Palindromic Substringlongest-palindromic-substringMedium33.4383Falsestring,dynamic-programmingalgorithmsYou can export the associated tables to a directory using theto_csvmethod:ls.to_csv(directory="<dir>")This generates 6.csvfiles in the current directory:questions.csv- List of questions with their title, difficulty, acceptance rate, paid status, topic tags, and category.companies.csv- List of companies with their name, slug, and the questions count.topicsTags.csv- List of topic tags with their name and slug.categories.csv- List of categories with their name and slug.questionCategory.csv- An edgelist of questions and their categories.questionTopics.csv- An edgelist of questions and their topic tags.Get Question statement and other informationQuery individual question's information such as the body, test cases, constraints, hints, code stubs, and company tags using theGetQuestionclass:fromleetscrapeimportGetQuestion# Get the question bodyquestion=GetQuestion(titleSlug="two-sum").scrape()This returns aQuestionobject with the following attributes:question.QID# Question IDquestion.title# Question titlequestion.titleSlug# Question title slugquestion.difficulty# Question difficultyquestion.Hints# Question hintsquestion.Companies# Question companiesquestion.topics# Question topic tagsquestion.SimilarQuestions# Similar questions idsquestion.Code# Code stubsquestion.Body# Question body / problem statementquestion.isPaidOnly# Whether the question is only available to premium users of LeetcodeGenerate code stubs for a questionfromleetscrapeimportGenerateCodeStub# Get the question bodyfcs=GenerateCodeStub(titleSlug="two-sum")fcs.generate(directory="<dir>")This generates the following files in the given directory:q_0001_twoSum.py- Python file with the code stub for the given question with a function namedtwoSum.test_q_0001_twoSum.py- Python file with the test cases for the given question.Seeexamplesfor examples of the generated code stubs.Generate mdx files from solutionsOnce you have solved a question, you can generate an mdx file with the solution and the question statement using theExtractSolutionsclass:fromleetscrapeimportExtractSolutions# Get the question bodysolutions=ExtractSolutions(filename="<path-to-solution-file>").extract()This outputs a list ofSolutionobjects with the following attributes:solution.id# Solution IDsolution.code# Solution codesolution.docs# Docstrings associated with the solutionsolution.problem_statement# Question body / problem statementAlternatively, you can use theto_mdxmethod to generate the mdx file:fromleetscrapeimportExtractSolutions# Get the question bodyExtractSolutions(filename="<path-to-solution-file>").to_mdx(output_filename="<path-to-output-file>")Serving the solutions withleetscrape-tsYou can use theleetscrape-tsNext.js template to serve the solutions. See thedemo. Visit the repo for more details. You can generate the project using theleetscrape tscommand:leetscrapets--out<path-to-output-directory>This will bootstrap the project in the given directory. Follow the instructions in theREADMEand create/modify the.env.localfile. Then, run the following command to generate the mdx files:leetscrapesolution--out<path-to-output-directory>/src/content/solutions<path-to-your-python-solution-directory>You can then run the project using the following command:cd<path-to-output-directory>
npmrundev# oryarndev# orpnpmdev# orbundevChangelogView the changeloghere.