1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

update;修复国外版脚本失效;修改国内版力扣地址

This commit is contained in:
程序员小墨 2022-05-02 23:30:08 +08:00
parent f3e61dc093
commit bc30d9a0a5
3 changed files with 9 additions and 7 deletions

View File

@ -1,6 +1,6 @@
# 力扣题库(完整版) # 力扣题库(完整版)
> 最后更新日期: **2022.04.03** > 最后更新日期: **2022.05.02**
> >
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件 > 使用脚本前请务必仔细完整阅读本 `README.md` 文件

View File

@ -34,7 +34,7 @@ def parse_proble_set(problemSet):
continue continue
def construct_url(problemTitle): def construct_url(problemTitle):
url = "https://leetcode-cn.com/problems/"+ problemTitle + "/" url = "https://leetcode.cn/problems/"+ problemTitle + "/"
# print(url) # print(url)
get_proble_content(url,problemTitle) get_proble_content(url,problemTitle)
@ -49,7 +49,7 @@ def save_problem(title,content, editorType = ""):
def get_proble_content(problemUrl,title): def get_proble_content(problemUrl,title):
# 随便请求一个页面获取csrf_token # 随便请求一个页面获取csrf_token
response = requests.get('https://leetcode-cn.com/graphql/', data = '''{"operationName":"userPremiumInfo","variables":{},"query":"query userPremiumInfo {\n userStatus {\n isPremium\n subscriptionPlanType\n __typename\n }\n}\n"}''') response = requests.get('https://leetcode.cn/graphql/', data = '''{"operationName":"userPremiumInfo","variables":{},"query":"query userPremiumInfo {\n userStatus {\n isPremium\n subscriptionPlanType\n __typename\n }\n}\n"}''')
setCookie = response.headers["set-cookie"] setCookie = response.headers["set-cookie"]
# print(setCookie) # print(setCookie)
''' '''
@ -61,7 +61,7 @@ def get_proble_content(problemUrl,title):
pattern = re.compile(".*?csrftoken=(.*?);.*?",re.S) pattern = re.compile(".*?csrftoken=(.*?);.*?",re.S)
csrftoken = re.search(pattern, setCookie) csrftoken = re.search(pattern, setCookie)
# print(csrftoken.group(1)) # print(csrftoken.group(1))
url = "https://leetcode-cn.com/graphql" url = "https://leetcode.cn/graphql"
data = { data = {
"operationName":"questionData", "operationName":"questionData",
"variables":{"titleSlug":title}, "variables":{"titleSlug":title},
@ -71,7 +71,7 @@ def get_proble_content(problemUrl,title):
'x-csrftoken': csrftoken.group(1), 'x-csrftoken': csrftoken.group(1),
'referer':problemUrl, 'referer':problemUrl,
'content-type':'application/json', 'content-type':'application/json',
'origin':'https://leetcode-cn.com', 'origin':'https://leetcode.cn',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36' 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'
} }
cookies = { cookies = {
@ -117,7 +117,7 @@ def saveJSON(data, filename):
json.dump(data, f, ensure_ascii=False, indent=4) json.dump(data, f, ensure_ascii=False, indent=4)
def main(): def main():
url = "https://leetcode-cn.com/api/problems/all/" url = "https://leetcode.cn/api/problems/all/"
html = json.loads(get_proble_set(url)) html = json.loads(get_proble_set(url))
saveJSON(html, "origin-data.json") saveJSON(html, "origin-data.json")

View File

@ -10,7 +10,9 @@ from bs4 import BeautifulSoup
def get_proble_set(url): def get_proble_set(url):
try: try:
response = requests.get(url) response = requests.get(url, headers = {
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41 Safari/537.36 Edg/101.0.1210.32"
})
if response.status_code == 200: if response.status_code == 200:
return response.text return response.text
return None return None