8
8
from requests .exceptions import RequestException
9
9
from bs4 import BeautifulSoup
10
10
11
+ import urllib3
12
+ urllib3 .disable_warnings (urllib3 .exceptions .InsecureRequestWarning )
13
+
11
14
def get_proble_set (url ):
12
15
try :
13
16
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"
15
18
}, verify = False )
16
19
if response .status_code == 200 :
17
20
return response .text
@@ -28,18 +31,23 @@ def parse_proble_set(problemSet):
28
31
print (i , "has been parsed." )
29
32
# print("The question has been parsed: {}".format(title))
30
33
continue
34
+ elif "paid_only" in problemSet [i ]:
35
+ paid_only = problemSet [i ]["paid_only" ]
36
+ if paid_only :
37
+ print (i , '付费题目,跳过' )
38
+ continue
31
39
#construct_url(http://github.com/title)
32
40
# time.sleep(0.5)
33
41
time .sleep (1 )
34
- t = threading .Thread (target = construct_url ,args = (title ,))
42
+ t = threading .Thread (target = construct_url , args = (title ,))
35
43
t .start ()
36
44
print (i , "is done." )
37
45
continue
38
46
39
47
def construct_url (problemTitle ):
40
- url = "https://leetcode.com/problems/" + problemTitle + "/description/"
48
+ url = "https://leetcode.com/problems/" + problemTitle + "/description/"
41
49
# print(url)
42
- get_proble_content (url ,problemTitle )
50
+ get_proble_content (url , problemTitle )
43
51
44
52
def save_problem (title ,content ):
45
53
#content = bytes(content,encoding = 'utf8')
@@ -49,16 +57,16 @@ def save_problem(title,content):
49
57
50
58
def get_proble_content (problemUrl ,title ):
51
59
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 )
54
62
setCookie = response .headers ["Set-Cookie" ]
55
63
'''
56
64
print(setCookie)
57
65
setCookie = json.loads(setCookie)
58
66
print(type(setCookie))
59
67
'''
60
68
try :
61
- pattern = re .compile ("csrftoken =(.*?);.*?" ,re .S )
69
+ pattern = re .compile ("__cf_bm =(.*?);.*?" ,re .S )
62
70
csrftoken = re .search (pattern , setCookie )
63
71
url = "https://leetcode.com/graphql"
64
72
data = {
@@ -105,7 +113,11 @@ def saveJSON(data, filename):
105
113
106
114
def main ():
107
115
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 )
109
121
saveJSON (html , "origin-data.json" )
110
122
111
123
# html = json.load(open("origin-data.json", 'r', encoding='utf-8'))
0 commit comments