Created
January 28, 2021 08:12
-
-
Save asukaminato0721/c73ba5c860fbf4cc8a9953f27e786519 to your computer and use it in GitHub Desktop.
leetcode 爬取每日一题官方题解
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Just for fun. | |
参考文章: https://wanakiki.github.io/2020/leetcode-spider/ | |
''' | |
import requests, json | |
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36" | |
url = "https://leetcode-cn.com/graphql" | |
session = requests.Session() | |
common_headers = { | |
"User-Agent": user_agent, | |
"Connection": "keep-alive", | |
"Content-Type": "application/json", | |
} | |
def 找到每日一题标题() -> str: | |
params = { | |
"operationName": "questionOfToday", | |
"variables": {}, | |
"query": """query questionOfToday { | |
todayRecord { | |
question { | |
questionFrontendId | |
questionTitleSlug | |
__typename | |
} | |
date | |
} | |
} | |
""", | |
} | |
json_data = json.dumps(params).encode("utf8") | |
headers = { | |
**common_headers, | |
"Referer": "https://leetcode-cn.com/problems/", | |
} | |
resp = session.post(url, data=json_data, headers=headers, timeout=10) | |
resp.encoding = "utf8" | |
return resp.json()["data"]["todayRecord"][0]["question"][ | |
"questionTitleSlug" | |
] | |
def 找到官方题解文章(slug: str): | |
params = { | |
"operationName": "questionSolutionArticles", | |
"variables": { | |
"questionSlug": slug, | |
"first": 1, | |
"skip": 0, | |
"orderBy": "DEFAULT", | |
}, | |
"query": """query questionSolutionArticles($questionSlug: String!, $skip: Int, $first: Int, $orderBy: SolutionArticleOrderBy, $userInput: String, $tagSlugs: [String!]) { | |
questionSolutionArticles(questionSlug: $questionSlug, skip: $skip, first: $first, orderBy: $orderBy, userInput: $userInput, tagSlugs: $tagSlugs) { | |
edges { | |
node { | |
...solutionArticle | |
} | |
} | |
} | |
} | |
fragment solutionArticle on SolutionArticleNode { | |
title | |
slug | |
tags { | |
name | |
nameTranslated | |
slug | |
} | |
} | |
""", | |
} | |
json_data = json.dumps(params).encode("utf8") | |
headers = { | |
**common_headers, | |
"Referer": f"https://leetcode-cn.com/problems/{slug}/solution/", | |
} | |
resp = session.post(url, data=json_data, headers=headers, timeout=10) | |
resp.encoding = "utf8" | |
return resp.json()["data"]["questionSolutionArticles"]["edges"][0]["node"][ | |
"slug" | |
] | |
def 找到官方题解代码(): | |
daily_problem = 找到每日一题标题() | |
题解文章 = 找到官方题解文章(daily_problem) | |
params = { | |
"operationName": "solutionDetailArticle", | |
"variables": { | |
"slug": 题解文章, | |
"orderBy": "DEFAULT", | |
}, | |
"query": """query solutionDetailArticle($slug: String!, $orderBy: SolutionArticleOrderBy!) { | |
solutionArticle(slug: $slug, orderBy: $orderBy) { | |
...solutionArticle | |
content | |
question { | |
questionTitleSlug | |
} | |
} | |
} | |
fragment solutionArticle on SolutionArticleNode { | |
title | |
slug | |
tags { | |
name | |
slug | |
} | |
summary | |
} | |
""", | |
} | |
json_data = json.dumps(params).encode("utf8") | |
headers = { | |
**common_headers, | |
"Referer": f"https://leetcode-cn.com/problems/{daily_problem}/solution/{题解文章}/", | |
} | |
resp = session.post(url, data=json_data, headers=headers, timeout=10) | |
resp.encoding = "utf8" | |
title = resp.json()["data"]["solutionArticle"]["content"] | |
print(title) | |
return title | |
找到官方题解代码() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment