-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode_problem_statement.py
51 lines (45 loc) · 1.46 KB
/
leetcode_problem_statement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import requests
import json
from bs4 import BeautifulSoup
def get_leetcode_problem_statement(slug):
url = "https://leetcode.com/graphql"
headers = {
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.3",
}
query = '''
query getQuestionDetail($titleSlug: String!) {
question(titleSlug: $titleSlug) {
questionId
title
content
difficulty
}
}
'''
variables = {
"titleSlug": slug
}
payload = {
"operationName": "getQuestionDetail",
"query": query,
"variables": variables
}
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 200:
data = json.loads(response.text)
problem = data['data']['question']
if problem:
soup = BeautifulSoup(problem['content'], 'html.parser')
content = "\n".join(soup.stripped_strings)
problem_statement = f"{problem['title']} (ID: {problem['questionId']}, Difficulty: {problem['difficulty']})\n\n{content}"
return problem_statement
else:
return "Problem not found."
else:
return f"Error: Unable to fetch problem (HTTP {response.status_code})."
'''
slug = "two-sum"
problem_statement = get_leetcode_problem_statement(slug)
print(problem_statement)
'''