Skip to content

Commit bc30d9a

Browse files
committed
update;修复国外版脚本失效;修改国内版力扣地址
1 parent f3e61dc commit bc30d9a

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 力扣题库(完整版)
22

3-
> 最后更新日期: **2022.04.03**
3+
> 最后更新日期: **2022.05.02**
44
>
55
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
66

Diff for: leetcode-cn.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def parse_proble_set(problemSet):
3434
continue
3535

3636
def construct_url(problemTitle):
37-
url = "https://leetcode-cn.com/problems/"+ problemTitle + "/"
37+
url = "https://leetcode.cn/problems/"+ problemTitle + "/"
3838
# print(url)
3939
get_proble_content(url,problemTitle)
4040

@@ -49,7 +49,7 @@ def save_problem(title,content, editorType = ""):
4949

5050
def get_proble_content(problemUrl,title):
5151
# 随便请求一个页面,获取csrf_token
52-
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"}''')
52+
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"}''')
5353
setCookie = response.headers["set-cookie"]
5454
# print(setCookie)
5555
'''
@@ -61,7 +61,7 @@ def get_proble_content(problemUrl,title):
6161
pattern = re.compile(".*?csrftoken=(.*?);.*?",re.S)
6262
csrftoken = re.search(pattern, setCookie)
6363
# print(csrftoken.group(1))
64-
url = "https://leetcode-cn.com/graphql"
64+
url = "https://leetcode.cn/graphql"
6565
data = {
6666
"operationName":"questionData",
6767
"variables":{"titleSlug":title},
@@ -71,7 +71,7 @@ def get_proble_content(problemUrl,title):
7171
'x-csrftoken': csrftoken.group(1),
7272
'referer':problemUrl,
7373
'content-type':'application/json',
74-
'origin':'https://leetcode-cn.com',
74+
'origin':'https://leetcode.cn',
7575
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'
7676
}
7777
cookies = {
@@ -117,7 +117,7 @@ def saveJSON(data, filename):
117117
json.dump(data, f, ensure_ascii=False, indent=4)
118118

119119
def main():
120-
url = "https://leetcode-cn.com/api/problems/all/"
120+
url = "https://leetcode.cn/api/problems/all/"
121121
html = json.loads(get_proble_set(url))
122122
saveJSON(html, "origin-data.json")
123123

Diff for: leetcode.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
def get_proble_set(url):
1212
try:
13-
response = requests.get(url)
13+
response = requests.get(url, headers = {
14+
'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"
15+
})
1416
if response.status_code == 200:
1517
return response.text
1618
return None

0 commit comments

Comments
 (0)