Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c70caac

Browse files
avinashkranjan#913 Codeforces_Problem_Scraper Added
1 parent 6914085 commit c70caac

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
from selenium import webdriver # Automated webdriver
3+
from PIL import Image
4+
from fpdf import FPDF # For converting images to pdf
5+
6+
7+
def getproblem():
8+
"""
9+
getproblem() : It takes input from the user of codeforces problemID and difficulty
10+
level and then by using selenium and chrome webdriver, capturing screenshot of the
11+
Codeforces problem using ttypography tag because all the problems of codeforces are
12+
stored inside this div tag and saving it in a image.png file.
13+
Then saving the image.png as pdf file by using fdf library.
14+
"""
15+
16+
# Taking input from the user to search for the problem
17+
Pblm_id = input("Enter the Problem ID: ")
18+
difficulty = input("Enter the difficulty level: ")
19+
filename = input('Enter the file name to store Question: ') + '.pdf'
20+
21+
# Going to the specific URL
22+
url = "https://codeforces.com/problemset/problem/" + Pblm_id + "/" + difficulty
23+
path = 'image.png'
24+
options = webdriver.ChromeOptions()
25+
26+
# Headless = True for taking a scrolling snapshot
27+
options.headless = True
28+
driver = webdriver.Chrome(r"chromedriver_win32\chromedriver.exe", options=options)
29+
driver.get(url)
30+
# Deciding height by tag
31+
required_height = driver.execute_script(
32+
'return document.body.parentNode.scrollHeight')
33+
driver.set_window_size(1366, required_height)
34+
35+
# Taking SS of everything within the ttypography class
36+
driver.find_element_by_class_name('ttypography').screenshot(path)
37+
38+
# Opening image with pillow so based to capture its height and width
39+
cover = Image.open(path)
40+
WIDTH, HEIGHT = cover.size
41+
MARGIN = 10
42+
# based on image's height and width we are adjusting the pdf margin and borders
43+
pdf = FPDF(unit='pt', format=[WIDTH + 2 * MARGIN, HEIGHT + 2 * MARGIN])
44+
pdf.add_page() # Adding new page to the pdf
45+
pdf.image(path, MARGIN, MARGIN)
46+
pdf.output(filename, "F") # saving the pdf with the specified filename
47+
48+
print(f'\nGreat Success!!! Check your directory for {filename} file!')
49+
50+
51+
if __name__ == "__main__":
52+
getproblem()
53+
os.remove('image.png')
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Save any Problem Statement you like from Codeforces as a PDF.
2+
3+
This python script will let you download Problem Statements from Codeforces and save them as a pdf file. The script uses Selenium Webdriver and fpdf library. Selenium is used with Chrome Webdriver, so having Chrome browser is a requirement.
4+
5+
## Setting up:
6+
7+
- Create a virtual environment and activate it.
8+
9+
- Install the requirements
10+
11+
```sh
12+
$ pip install -r requirements.txt
13+
```
14+
15+
## Running the script:
16+
17+
```sh
18+
$ python Codeforces_problem_scrapper.py
19+
```
20+
21+
## Terminal Screenshot:
22+
23+
![Imgur](https://i.imgur.com/Qr0AwMG.png)
24+
25+
The program will ask you to enter:
26+
1. Valid Problem ID.
27+
2. Valid Difficulty Level.
28+
3. filename(without '.pdf'). The pdf will be created in the same folder.
29+
30+
## PDF Output:
31+
![Imgur](https://i.imgur.com/GpSxCRZ.png)
32+
![Imgur](https://i.imgur.com/c5mCNWM.png)
33+
34+
## Author
35+
[Akhil Bhalerao](https://github.com/iamakkkhil)
36+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pillow
2+
fpdf
3+
selenium
4+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /