♚\
KivenC,一名爱好Python编程的机械男。
GitHub:github.com/KivenCkl
LeetCode简介
leetcode是一个美国的在线编程网站,它收集了各大公司的经典算法面试题,用户可以选择不同的语言进行代码的在线编写、编译和调试。简单来说,它就是程序员的刷题神器。
概述
项目地址:Leetcode_Helper
https://github.com/KivenCkl/Leetcode_Helper
Python 实现的 LeetCode 仓库美化程序。爬取 LeetCode-cn AC 的题目描述和提交的最新代码,并整理至相应的文件夹,生成相应的 README 文件。
项目结构:
LeetCode_Helper/
|-- db/
| |-- leetcode.db(数据库)
|
|-- helper/
| |-- __init__.py
| |-- config.py(解析配置文件)
| |-- constants.py(常数)
| |-- extractor.py(数据导出类)
| |-- login.py(模拟登录)
| |-- main.py(主程序)
| |-- node.py(解析爬虫获取的数据)
| |-- problems.py(主逻辑)
| |-- templates.py(模板)
| |-- utils.py(工具箱)
|
|-- imgs/
|
|-- tests/
|
|-- config.json(配置文件)
|-- run.py(入口程序)
|-- README.md
本项目参考了:
leetcode-spider
https://github.com/zhantong/leetcode-spider
LeetCodeCrawler
https://github.com/ZhaoxiZhang/LeetCodeCrawler
特点
- 支持爬取题目列表(中英文),保存为指定目录下的 README 和 README_EN 文件
- 支持爬取题目描述(中英文),保存为对应 title 文件夹下的 README 和 README_EN 文件
- 支持爬取用户提交的代码,保存为对应 title 文件夹下的 AC 源码(可以是任意语言)
- 支持修改导出数据的模板
- 异步下载题目描述,高速并发导出文件
- 支持增量更新,当 LeetCode-cn 有新内容(题目/提交的代码)时,可以选择增量形式更新
核心思路
-
获取 LeetCode-cn 用户 cookies
class Login: ''' 登录 LeetCode-cn, 获取 cookies 值 ''' def __init__(self, username, password): self.username = username self.password = password self.__cookies = '' self.status = False def doLogin(self): resp = requests.get(LEETCODE, headers=HEADERS) token = resp.cookies['csrftoken'] headers = HEADERS.copy() headers.update({ 'referer': LOGIN, 'x-csrftoken': token, 'x-requested-with': 'XMLHttpRequest' }) payload = { 'login': self.username, 'password': self.password, 'csrfmiddlewaretoken': token } cookies = {'csrftoken': token} resp = requests.post( LOGIN, data=payload, headers=headers, cookies=cookies) if resp.status_code == 200: self.status = True self.__cookies = resp.cookies # user = resp.json()['form']['fields']['login']['value'] if self.status: print(f'{self.username} 登录成功!') else: print('登录失败!') print('请检查用户名和密码!') @property def cookies(self): if not self.status: self.doLogin() return self.__cookies -
从 leetcode-cn.com/api/problem… 获取用户解题基本信息以及问题列表
def __getProblemsJson(self): resp = requests.get(PROBLEMS, headers=HEADERS, cookies=self.__cookies) if resp.status_code == 200: return resp.json() -
从 leetcode-cn.com/graphql 异步访问获取问题描述信息
async def __getProblemDesc(self, title_slug): payload = { 'query': ''' query questionData(¨E123Equestion(titleSlug:<annotation encoding="application style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;" span="" class="hljs-string" br="" encoding=""application"><span class="katex-html" aria-hidden="true" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">t<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">i<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">t<span class="mord mathit" style="margin-right:0.01968em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">l<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">e<span class="mord mathit" style="margin-right:0.05764em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">S<span class="mord mathit" style="margin-right:0.01968em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">l<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">u<span class="mord mathit" style="margin-right:0.03588em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">g<span class="mspace" style="margin-right:0.2777777777777778em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">:<span class="mspace" style="margin-right:0.2777777777777778em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="strut" style="height:1em;vertical-align:-0.25em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="mord mathit" style="margin-right:0.05764em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">S<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">t<span class="mord mathit" style="margin-right:0.02778em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">r<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">i<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">n<span class="mord mathit" style="margin-right:0.03588em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">g!)¨<span class="mord mathit" style="margin-right:0.05764em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E123<span class="mord mathit" style="margin-right:0.05764em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E<span class="mord mathit" style="margin-right:0.03588em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">q<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">u<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">e<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">s<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">t<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">i<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">o<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">n(<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">t<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">i<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">t<span class="mord mathit" style="margin-right:0.01968em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">l<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">e<span class="mord mathit" style="margin-right:0.05764em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">S<span class="mord mathit" style="margin-right:0.01968em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">l<span class="mord mathit" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">u<span class="mord mathit" style="margin-right:0.03588em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">g<span class="mspace" style="margin-right:0.2777777777777778em;" style="box-sizing: border-box;font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">:titleSlug) { questionId content translatedTitle translatedContent similarQuestions topicTags { name slug translatedName } hints } } ''', 'operationName': 'questionData', 'variables': { 'titleSlug': title_slug } } async with aiohttp.ClientSession(cookies=self.__cookies) as session: async with session.post( GRAPHQL, json=payload, headers=HEADERS) as resp: return await resp.json() </span class="mspace" style="margin-right:0.2777777777777778em;"></span class="mord mathit" style="margin-right:0.03588em;"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.01968em;"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.01968em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.03588em;"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit" style="margin-right:0.03588em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.05764em;"></span class="strut" style="height:1em;vertical-align:-0.25em;"></span class="mspace" style="margin-right:0.2777777777777778em;"></span class="mspace" style="margin-right:0.2777777777777778em;"></span class="mord mathit" style="margin-right:0.03588em;"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.01968em;"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.01968em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span class="katex-html" aria-hidden="true"></annotation encoding="application> -
从 leetcode-cn.com/api/submiss… 获取提交的代码信息
def __getSubmissions(self): result = [] offset = 0 while True: resp = requests.get( SUBMISSIONS_FORMAT.format(offset), headers=HEADERS, cookies=self.__cookies) content = resp.json() result.extend(content['submissions_dump']) # 判断是否还有下一页 if not content['has_next']: return result offset += 20 -
将获取到的数据进行解析,详见
node.py -
用 sqlite3 将解析后的数据存储至数据库,详见
problems.py -
从数据库中取出数据生成所需文件,详见
extractor.py
使用
使用 git clone 或直接下载本仓库代码至本地
本项目需要用到第三方库 requests 和 aiohttp,可通过 pip 命令安装。
运行 python run.py
效果
具体效果以及爬取的具体数据可参看 repo: LeetCode
https://github.com/KivenCkl/LeetCode
你可以根据你自己的需求爱好修改 templates.py 其中的模板
可以修改其根目录下的 config.json 文件:
{
"username": "leetcode-cn@leetcode",
"password": "leetcode",
"outputDir": "../LeetCode"
}
username和password对应你的 LeetCode-cn 账号和密码outputDir对应你希望存放源码文件的目录
\
zhi
支
chi
持
zuo
作
zhe
者
\
\
长按扫码鼓励作者
\
热 门 推 荐
用Python查看微信共同好友
用Python获取摄像头并实时控制人脸
用 Python 验证股神巴菲特的投资经验
开源项目 | 如何免费创建云端爬虫集群
用Python分析《都挺好》中的人物关系
推荐Python中文社区旗下的几个服务类公众号\
▼ 点击成为社区注册会员 **「在看」**一下,一起PY!