Skip to content

Commit 847e599

Browse files
author
zhangbk1
committed
忽略https警告;国外版力扣题库拉取失败问题修复;其他小问题修复
1 parent b1fc2c6 commit 847e599

File tree

3 files changed

+80
-58
lines changed

3 files changed

+80
-58
lines changed

README.md

+46-46
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
1-
# 力扣题库(完整版)
2-
3-
> 最后更新日期: **2024.06.26**
4-
>
5-
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
6-
7-
### 仓库介绍
8-
9-
使用 Python 脚本分批将力扣(`leetcode.com``leetcode-cn.com`)上面的题目保存下来,方便没有网的时候进行学习。(仅包含可以在网页上直接打开的公开题目,不包含 VIP 题目)
10-
11-
12-
13-
### 仓库目录结构
14-
15-
国外版力扣题库,在仓库 `leetcode/problem` 文件夹下;国内版力扣题库,在仓库 `leetcode-cn/problem (Chinese)``leetcode-cn/problem (English)` 文件夹下。(部分题目只有中文版,无对应英文版)。
16-
17-
18-
19-
### 特别注意!
20-
21-
#### 版权相关
22-
23-
**所有版权都为 LeetCode (及力扣中国) 官方所有,此处仅供学习使用,不要他用。也请大家不要滥用,不要侵犯力扣平台的合法权益。**
24-
25-
**感谢 LeetCode 平台为我们提供大量的算法题目进行练习与提升。如果大家经济条件允许,请大家多多支持力扣,例如冲冲会员等**
26-
27-
力扣题库的权益归属力扣,使用力扣题库,需要遵循力扣使用条例,若您不同意此条例,请立即关闭当前网页,不要继续使用本题库。
28-
29-
力扣(LeetCode)• 使用条例: https://leetcode-cn.com/terms-c/
30-
31-
LeetCode Terms of Service: https://leetcode.com/terms/
32-
33-
34-
35-
#### 脚本原作者
36-
37-
Python脚本是在网上教程的基础上进行二改得到的,原版地址:https://blog.csdn.net/weixin_37267014/article/details/81429057
38-
39-
40-
41-
#### 其他
42-
43-
由于脚本运行时会向力扣网站发出大量请求,所以请大家不要随便尝试此脚本,以免影响力扣网站正常运行。
44-
45-
因为使用此脚本所造成的一系列问题,责任由您自己承担,作者不承担相应责任。
46-
1+
# 力扣题库(完整版)
2+
3+
> 最后更新日期: **2024.06.26**
4+
>
5+
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
6+
7+
### 仓库介绍
8+
9+
使用 Python 脚本分批将力扣(`leetcode.com``leetcode-cn.com`)上面的题目保存下来,方便没有网的时候进行学习。(仅包含可以在网页上直接打开的公开题目,不包含 VIP 题目)
10+
11+
12+
13+
### 仓库目录结构
14+
15+
国外版力扣题库,在仓库 `leetcode/problem` 文件夹下;国内版力扣题库,在仓库 `leetcode-cn/problem (Chinese)``leetcode-cn/problem (English)` 文件夹下。(部分题目只有中文版,无对应英文版)。
16+
17+
18+
19+
### 特别注意!
20+
21+
#### 版权相关
22+
23+
**所有版权都为 LeetCode (及力扣中国) 官方所有,此处仅供学习使用,不要他用。也请大家不要滥用,不要侵犯力扣平台的合法权益。**
24+
25+
**感谢 LeetCode 平台为我们提供大量的算法题目进行练习与提升。如果大家经济条件允许,请大家多多支持力扣,例如充充会员等**
26+
27+
力扣题库的权益归属力扣,使用力扣题库,需要遵循力扣使用条例,若您不同意此条例,请立即关闭当前网页,不要继续使用本题库。
28+
29+
力扣(LeetCode)• 使用条例: https://leetcode-cn.com/terms-c/
30+
31+
LeetCode Terms of Service: https://leetcode.com/terms/
32+
33+
34+
35+
#### 脚本原作者
36+
37+
Python脚本是在网上教程的基础上进行二改得到的,原版地址:https://blog.csdn.net/weixin_37267014/article/details/81429057
38+
39+
40+
41+
#### 其他
42+
43+
由于脚本运行时会向力扣网站发出大量请求,所以请大家不要随便尝试此脚本,以免影响力扣网站正常运行。
44+
45+
因为使用此脚本所造成的一系列问题,责任由您自己承担,作者不承担相应责任。
46+

leetcode-cn.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88
from requests.exceptions import RequestException
99
from bs4 import BeautifulSoup
1010

11+
import urllib3
12+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
13+
1114
def get_proble_set(url):
1215
try:
13-
response = requests.get(url)
16+
# response = requests.get(url)
17+
response = requests.get(url, headers = {
18+
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
19+
}, verify=False)
1420
if response.status_code == 200:
1521
return response.text
1622
return None
@@ -34,9 +40,9 @@ def parse_proble_set(problemSet):
3440
continue
3541

3642
def construct_url(problemTitle):
37-
url = "https://leetcode.cn/problems/"+ problemTitle + "/"
43+
url = "https://leetcode.cn/problems/" + problemTitle + "/"
3844
# print(url)
39-
get_proble_content(url,problemTitle)
45+
get_proble_content(url, problemTitle)
4046

4147
def save_problem(title,content, editorType = ""):
4248
#content = bytes(content,encoding = 'utf8')
@@ -118,7 +124,11 @@ def saveJSON(data, filename):
118124

119125
def main():
120126
url = "https://leetcode.cn/api/problems/all/"
121-
html = json.loads(get_proble_set(url))
127+
jsonContent = get_proble_set(url)
128+
if jsonContent == None:
129+
print('列表请求失败!')
130+
return
131+
html = json.loads(jsonContent)
122132
saveJSON(html, "origin-data.json")
123133

124134
# html = json.load(open("origin-data.json", 'r', encoding='utf-8'))

leetcode.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
from requests.exceptions import RequestException
99
from bs4 import BeautifulSoup
1010

11+
import urllib3
12+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
13+
1114
def get_proble_set(url):
1215
try:
1316
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"
17+
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
1518
}, verify=False)
1619
if response.status_code == 200:
1720
return response.text
@@ -28,18 +31,23 @@ def parse_proble_set(problemSet):
2831
print(i, "has been parsed.")
2932
# print("The question has been parsed: {}".format(title))
3033
continue
34+
elif "paid_only" in problemSet[i]:
35+
paid_only = problemSet[i]["paid_only"]
36+
if paid_only:
37+
print(i, '付费题目,跳过')
38+
continue
3139
#construct_url(http://github.com/title)
3240
# time.sleep(0.5)
3341
time.sleep(1)
34-
t =threading.Thread(target=construct_url,args=(title,))
42+
t = threading.Thread(target=construct_url, args=(title,))
3543
t.start()
3644
print(i, "is done.")
3745
continue
3846

3947
def construct_url(problemTitle):
40-
url = "https://leetcode.com/problems/"+ problemTitle + "/description/"
48+
url = "https://leetcode.com/problems/" + problemTitle + "/description/"
4149
# print(url)
42-
get_proble_content(url,problemTitle)
50+
get_proble_content(url, problemTitle)
4351

4452
def save_problem(title,content):
4553
#content = bytes(content,encoding = 'utf8')
@@ -49,16 +57,16 @@ def save_problem(title,content):
4957

5058
def get_proble_content(problemUrl,title):
5159
response = requests.get(problemUrl, headers = {
52-
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36"
53-
})
60+
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
61+
}, verify=False)
5462
setCookie = response.headers["Set-Cookie"]
5563
'''
5664
print(setCookie)
5765
setCookie = json.loads(setCookie)
5866
print(type(setCookie))
5967
'''
6068
try:
61-
pattern = re.compile("csrftoken=(.*?);.*?",re.S)
69+
pattern = re.compile("__cf_bm=(.*?);.*?",re.S)
6270
csrftoken = re.search(pattern, setCookie)
6371
url = "https://leetcode.com/graphql"
6472
data = {
@@ -105,7 +113,11 @@ def saveJSON(data, filename):
105113

106114
def main():
107115
url = "https://leetcode.com/api/problems/all/"
108-
html = json.loads(get_proble_set(url))
116+
jsonContent = get_proble_set(url)
117+
if jsonContent == None:
118+
print('列表请求失败!')
119+
return
120+
html = json.loads(jsonContent)
109121
saveJSON(html, "origin-data.json")
110122

111123
# html = json.load(open("origin-data.json", 'r', encoding='utf-8'))

0 commit comments

Comments
 (0)