mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 10:38:13 +08:00
update;修复国外版脚本失效;修改国内版力扣地址
This commit is contained in:
parent
f3e61dc093
commit
bc30d9a0a5
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2022.04.03**
|
||||
> 最后更新日期: **2022.05.02**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
@ -34,7 +34,7 @@ def parse_proble_set(problemSet):
|
||||
continue
|
||||
|
||||
def construct_url(problemTitle):
|
||||
url = "https://leetcode-cn.com/problems/"+ problemTitle + "/"
|
||||
url = "https://leetcode.cn/problems/"+ problemTitle + "/"
|
||||
# print(url)
|
||||
get_proble_content(url,problemTitle)
|
||||
|
||||
@ -49,7 +49,7 @@ def save_problem(title,content, editorType = ""):
|
||||
|
||||
def get_proble_content(problemUrl,title):
|
||||
# 随便请求一个页面,获取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"]
|
||||
# print(setCookie)
|
||||
'''
|
||||
@ -61,7 +61,7 @@ def get_proble_content(problemUrl,title):
|
||||
pattern = re.compile(".*?csrftoken=(.*?);.*?",re.S)
|
||||
csrftoken = re.search(pattern, setCookie)
|
||||
# print(csrftoken.group(1))
|
||||
url = "https://leetcode-cn.com/graphql"
|
||||
url = "https://leetcode.cn/graphql"
|
||||
data = {
|
||||
"operationName":"questionData",
|
||||
"variables":{"titleSlug":title},
|
||||
@ -71,7 +71,7 @@ def get_proble_content(problemUrl,title):
|
||||
'x-csrftoken': csrftoken.group(1),
|
||||
'referer':problemUrl,
|
||||
'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'
|
||||
}
|
||||
cookies = {
|
||||
@ -117,7 +117,7 @@ def saveJSON(data, filename):
|
||||
json.dump(data, f, ensure_ascii=False, indent=4)
|
||||
|
||||
def main():
|
||||
url = "https://leetcode-cn.com/api/problems/all/"
|
||||
url = "https://leetcode.cn/api/problems/all/"
|
||||
html = json.loads(get_proble_set(url))
|
||||
saveJSON(html, "origin-data.json")
|
||||
|
||||
|
@ -10,7 +10,9 @@ from bs4 import BeautifulSoup
|
||||
|
||||
def get_proble_set(url):
|
||||
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:
|
||||
return response.text
|
||||
return None
|
||||
|
Loading…
Reference in New Issue
Block a user