-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
35 lines (27 loc) · 898 Bytes
/
scraper.py
File metadata and controls
35 lines (27 loc) · 898 Bytes
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
import requests
from bs4 import BeautifulSoup
class WebSudokuScraper:
BASE_URL = "http://nine.websudoku.com/"
EMPTY_CELL = 0
def __init__(self):
pass
def get_grid(self, level=1):
page = self._get_page(level)
soup = BeautifulSoup(page.content, "html.parser")
grid_elem = soup.find(id='puzzle_grid')
grid = []
for row_elem in grid_elem:
_row = []
for cell_elem in row_elem:
try:
value = cell_elem.next_element['value']
_row.append(int(value))
except KeyError:
_row.append(self.EMPTY_CELL)
grid.append(_row)
return grid
def _get_page(self, level=1):
level_param = f"level={level}"
url = f"{self.BASE_URL}?{level_param}"
page = requests.get(url)
return page