Base code for estonian language
Browse files- .gitattributes +1 -0
- .gitignore +6 -0
- estonian_language_exams.ipynb +1696 -0
- estonian_language_exams/Diagnoostestid.pdf +3 -0
- estonian_language_exams/Sõeltestid.pdf +3 -0
- estonian_language_exams/complex_data.pkl +3 -0
- estonian_language_exams/simple_data.pkl +3 -0
- utils_sel.py +152 -0
.gitattributes
CHANGED
@@ -56,3 +56,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
56 |
# Video files - compressed
|
57 |
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
58 |
*.webm filter=lfs diff=lfs merge=lfs -text
|
|
|
|
56 |
# Video files - compressed
|
57 |
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
58 |
*.webm filter=lfs diff=lfs merge=lfs -text
|
59 |
+
*.pdf filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.ipynb_checkpoints
|
2 |
+
*/.ipynb_checkpoints/*
|
3 |
+
|
4 |
+
# IPython
|
5 |
+
profile_default/
|
6 |
+
ipython_config.py
|
estonian_language_exams.ipynb
ADDED
@@ -0,0 +1,1696 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"id": "6522ba5f-13ea-419d-ac40-52b75271704c",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"import re\n",
|
11 |
+
"import json\n",
|
12 |
+
"import pickle\n",
|
13 |
+
"from pathlib import Path\n",
|
14 |
+
"from collections import OrderedDict, defaultdict\n",
|
15 |
+
"from tqdm import tqdm\n",
|
16 |
+
"from utils_sel import *\n",
|
17 |
+
"from bs4 import BeautifulSoup, NavigableString, Tag, Comment\n",
|
18 |
+
"from selenium.webdriver.common.by import By\n",
|
19 |
+
"from selenium.webdriver.support.ui import WebDriverWait\n",
|
20 |
+
"from selenium.webdriver.support import expected_conditions as EC\n",
|
21 |
+
"from selenium.common.exceptions import NoSuchElementException"
|
22 |
+
]
|
23 |
+
},
|
24 |
+
{
|
25 |
+
"cell_type": "markdown",
|
26 |
+
"id": "c06982c7-12fe-41d3-831e-c169514c4f70",
|
27 |
+
"metadata": {},
|
28 |
+
"source": [
|
29 |
+
"## Local functions"
|
30 |
+
]
|
31 |
+
},
|
32 |
+
{
|
33 |
+
"cell_type": "code",
|
34 |
+
"execution_count": null,
|
35 |
+
"id": "7f0927ab-17f1-4e2f-bc9a-df5e3438901c",
|
36 |
+
"metadata": {},
|
37 |
+
"outputs": [],
|
38 |
+
"source": [
|
39 |
+
"HOME_URL = \"https://web.meis.ee/testest/goto.php?target=root_1&client_id=integratsioon\"\n",
|
40 |
+
"SAVE_PATH = Path('./estonian_language_exams/')\n",
|
41 |
+
"\n",
|
42 |
+
"def initialize(production=True):\n",
|
43 |
+
" driver = get_driver(production)\n",
|
44 |
+
" driver.get(HOME_URL)\n",
|
45 |
+
" close_popup = driver.find_element(By.ID, value='sb-nav-close')\n",
|
46 |
+
" WebDriverWait(driver, 10).until(EC.element_to_be_clickable(close_popup))\n",
|
47 |
+
" close_popup.click()\n",
|
48 |
+
" return driver\n",
|
49 |
+
"\n",
|
50 |
+
"def get_tests(driver):\n",
|
51 |
+
" tests = driver.find_element(By.CLASS_NAME, 'ilContainerItemsContainer').find_elements(By.TAG_NAME, 'h4')\n",
|
52 |
+
" tests = [elem.find_element(By.TAG_NAME, 'a') for elem in tests]\n",
|
53 |
+
" links = [elem.get_attribute('href') for elem in tests]\n",
|
54 |
+
" names = [elem.text for elem in tests]\n",
|
55 |
+
" return links, names\n",
|
56 |
+
"\n",
|
57 |
+
"def start_test(driver):\n",
|
58 |
+
" click_button(driver, 'cmd[startPlayer]')\n",
|
59 |
+
" click_button(driver, 'cmd[accessCodeConfirmed]')\n",
|
60 |
+
"\n",
|
61 |
+
"def finish_test(driver):\n",
|
62 |
+
" click_link(driver, 'Lõpeta test')\n",
|
63 |
+
" click_link(driver, 'Vaata vastuseid')\n",
|
64 |
+
"\n",
|
65 |
+
"def get_test_table(driver):\n",
|
66 |
+
" table = driver.find_element(By.CLASS_NAME, 'table-striped').find_element(By.TAG_NAME, 'tbody')\n",
|
67 |
+
" questions = table.find_elements(By.TAG_NAME, 'a')\n",
|
68 |
+
" links = [elem.get_attribute('href') for elem in questions]\n",
|
69 |
+
" names = [elem.text for elem in questions]\n",
|
70 |
+
" ids = [int(elem.find_elements(By.TAG_NAME, 'td')[1].text) for elem in table.find_elements(By.TAG_NAME, 'tr')]\n",
|
71 |
+
" return links, names, ids\n",
|
72 |
+
"\n",
|
73 |
+
"def get_answers_single_type(driver):\n",
|
74 |
+
" instruction = driver.find_element(By.CLASS_NAME, 'ilc_heading3_Headline3').text\n",
|
75 |
+
" questions_answers = driver.find_elements(By.CLASS_NAME, 'ilc_question_SingleChoice')\n",
|
76 |
+
" question = questions_answers[0].find_element(By.CLASS_NAME, 'ilc_qtitle_Title').text\n",
|
77 |
+
" answers = questions_answers[1].find_elements(By.CLASS_NAME, 'ilc_qanswer_Answer')\n",
|
78 |
+
" options = [elem.text.strip() for elem in answers]\n",
|
79 |
+
" correct = [i+1 for i, elem in enumerate(answers) if elem.find_element(By.TAG_NAME, 'img').get_attribute('alt')=='Kontrollitud']\n",
|
80 |
+
" assert len((instruction + question).strip()) > 0\n",
|
81 |
+
" assert len(correct) == 1\n",
|
82 |
+
" return instruction, question, options, str(correct[0])\n",
|
83 |
+
"\n",
|
84 |
+
"def get_page_title_html(driver):\n",
|
85 |
+
" page = driver.find_element(By.ID, 'ilc_Page')\n",
|
86 |
+
" page_html = get_html_with_images(driver, page)\n",
|
87 |
+
" page_name = page.find_element(By.CLASS_NAME, 'ilc_page_title_PageTitle').text.strip()\n",
|
88 |
+
" return page_name, page_html\n",
|
89 |
+
"\n",
|
90 |
+
"def toggle_top_bar(driver, visible=False):\n",
|
91 |
+
" script = f\"\"\"\n",
|
92 |
+
" var topBars = document.getElementsByClassName('ilTopFixed');\n",
|
93 |
+
" for (var i = 0; i < topBars.length; i++) {{\n",
|
94 |
+
" topBars[i].style.display = '{'' if visible else 'none'}';\n",
|
95 |
+
" topBars[i].style.visibility = '{'' if visible else 'hidden'}';\n",
|
96 |
+
" }}\n",
|
97 |
+
" \"\"\"\n",
|
98 |
+
" driver.execute_script(script)\n",
|
99 |
+
"\n",
|
100 |
+
"def hide_own_answer(driver):\n",
|
101 |
+
" questions = driver.find_elements(By.CLASS_NAME, 'ilc_question_Standard')\n",
|
102 |
+
" instruction = driver.find_element(By.CLASS_NAME, 'ilc_qtitle_Title')\n",
|
103 |
+
" copy_element_as_first_child(driver, instruction, questions[1].find_element(By.XPATH, \"./div\"))\n",
|
104 |
+
" hide_between_start_end(driver, questions[0].get_property('parentNode'), 'Sinu vastus oli:', 'Õige vastus on:')\n",
|
105 |
+
"\n",
|
106 |
+
"def get_page_screenshot(driver, remove_own=True, as_bytes=True):\n",
|
107 |
+
" toggle_top_bar(driver)\n",
|
108 |
+
" div = driver.find_element(By.ID, 'ilc_Page')\n",
|
109 |
+
" if remove_own:\n",
|
110 |
+
" hide_own_answer(driver)\n",
|
111 |
+
" im = take_full_div_screenshot(driver, div)\n",
|
112 |
+
" if as_bytes:\n",
|
113 |
+
" return image_to_bytes(im)\n",
|
114 |
+
" return im\n",
|
115 |
+
"\n",
|
116 |
+
"def get_first_num_from_title(title):\n",
|
117 |
+
" match = re.search(r'\\s(\\d+)\\.\\s', title)\n",
|
118 |
+
" return int(match.group(1))\n",
|
119 |
+
"\n",
|
120 |
+
"def save_all_questions(driver, save_dict):\n",
|
121 |
+
" continue_questions = True\n",
|
122 |
+
" while continue_questions:\n",
|
123 |
+
" title, html = get_page_title_html(driver)\n",
|
124 |
+
" save_dict[title] = {'html_question': html}\n",
|
125 |
+
" try:\n",
|
126 |
+
" click_link(driver, 'Järgmine')\n",
|
127 |
+
" except NoSuchElementException:\n",
|
128 |
+
" continue_questions = False\n",
|
129 |
+
"\n",
|
130 |
+
"def nums_from_title(title):\n",
|
131 |
+
" matches = re.findall(r'(\\d+)\\.', title)\n",
|
132 |
+
" nums = [int(match) for match in matches][1:]\n",
|
133 |
+
" if len(nums) == 2:\n",
|
134 |
+
" nums = list(range(nums[0], nums[1]+1))\n",
|
135 |
+
" return nums\n",
|
136 |
+
"\n",
|
137 |
+
"def extract_number_and_text(string):\n",
|
138 |
+
" match = re.match(r'^(\\d+)\\.\\s(.+)$', string)\n",
|
139 |
+
" if match:\n",
|
140 |
+
" return int(match.group(1)), match.group(2)\n",
|
141 |
+
" else:\n",
|
142 |
+
" return None\n",
|
143 |
+
"\n",
|
144 |
+
"def extract_letter_and_text(string):\n",
|
145 |
+
" match = re.match(r'^([A-Z]+)\\s+(.+)$', string)\n",
|
146 |
+
" if match:\n",
|
147 |
+
" return match.group(1), match.group(2)\n",
|
148 |
+
" else:\n",
|
149 |
+
" return None\n",
|
150 |
+
"\n",
|
151 |
+
"def has_repeated(any_list):\n",
|
152 |
+
" return len(any_list) != len(set(any_list))\n",
|
153 |
+
"\n",
|
154 |
+
"def sep_num_ABC_mult(text):\n",
|
155 |
+
" quests = []\n",
|
156 |
+
" opts_dic = defaultdict(list)\n",
|
157 |
+
" for elem in text.split('\\n'):\n",
|
158 |
+
" if txt := elem.strip():\n",
|
159 |
+
" if num_txt := extract_number_and_text(txt):\n",
|
160 |
+
" quests.append(num_txt)\n",
|
161 |
+
" elif alf_txt := extract_letter_and_text(txt):\n",
|
162 |
+
" opts_dic[quests[-1][0]].append(alf_txt)\n",
|
163 |
+
" assert all(len(o) > 1 for o in opts_dic.values())\n",
|
164 |
+
" opts_list = [opts_dic[q[0]] for q in quests]\n",
|
165 |
+
" return quests, [[s[0] for s in ops] for ops in opts_list], [[s[1] for s in ops] for ops in opts_list]"
|
166 |
+
]
|
167 |
+
},
|
168 |
+
{
|
169 |
+
"cell_type": "markdown",
|
170 |
+
"id": "149a8bc9-c2f9-44f7-937c-66cb1263cf69",
|
171 |
+
"metadata": {},
|
172 |
+
"source": [
|
173 |
+
"## The simple and nicely formated MQA questions"
|
174 |
+
]
|
175 |
+
},
|
176 |
+
{
|
177 |
+
"cell_type": "code",
|
178 |
+
"execution_count": null,
|
179 |
+
"id": "00b91c64-88e0-43fb-860a-7435b260a685",
|
180 |
+
"metadata": {
|
181 |
+
"scrolled": true
|
182 |
+
},
|
183 |
+
"outputs": [],
|
184 |
+
"source": [
|
185 |
+
"sd_pkl_path = SAVE_PATH / 'simple_data.pkl'\n",
|
186 |
+
"if sd_pkl_path.exists():\n",
|
187 |
+
" with sd_pkl_path.open('rb') as file:\n",
|
188 |
+
" simple_data = pickle.load(file)\n",
|
189 |
+
"else:\n",
|
190 |
+
" simple_data = OrderedDict()\n",
|
191 |
+
" driver = initialize()\n",
|
192 |
+
" click_link(driver, 'Sõeltestid')\n",
|
193 |
+
" tests_links, tests_names = get_tests(driver)\n",
|
194 |
+
" for test_link, test_name in zip(tests_links, tests_names):\n",
|
195 |
+
" simple_data[test_name] = OrderedDict()\n",
|
196 |
+
" driver.get(test_link)\n",
|
197 |
+
" start_test(driver)\n",
|
198 |
+
" finish_test(driver)\n",
|
199 |
+
" links, names, ids = get_test_table(driver)\n",
|
200 |
+
" for url, title, id in zip(links, tqdm(names), ids):\n",
|
201 |
+
" driver.get(url)\n",
|
202 |
+
" instruction, question, options, correct = get_answers_single_type(driver)\n",
|
203 |
+
" simple_data[test_name][id] = {\n",
|
204 |
+
" \"category_original_lang\": test_name,\n",
|
205 |
+
" \"original_question_num\": get_first_num_from_title(title),\n",
|
206 |
+
" \"question\": instruction + '\\n' + question,\n",
|
207 |
+
" \"options\": options,\n",
|
208 |
+
" \"answer\": correct,\n",
|
209 |
+
" \"html\": get_page_title_html(driver)[1],\n",
|
210 |
+
" \"image\": get_page_screenshot(driver)\n",
|
211 |
+
" }\n",
|
212 |
+
" driver.close()\n",
|
213 |
+
" with sd_pkl_path.open('wb') as file:\n",
|
214 |
+
" pickle.dump(simple_data, file)\n",
|
215 |
+
" image_list = []\n",
|
216 |
+
" for test_name in simple_data.keys():\n",
|
217 |
+
" image_list.append(create_image_from_text(test_name))\n",
|
218 |
+
" for id in simple_data[test_name].keys():\n",
|
219 |
+
" image_list.append(simple_data[test_name][id]['image'])\n",
|
220 |
+
" create_pdf_from_images(image_list, SAVE_PATH / 'Sõeltestid.pdf')"
|
221 |
+
]
|
222 |
+
},
|
223 |
+
{
|
224 |
+
"cell_type": "markdown",
|
225 |
+
"id": "8dff6bef-8864-415e-ae7f-7cb815c47038",
|
226 |
+
"metadata": {},
|
227 |
+
"source": [
|
228 |
+
"## Questions that have varying formatting"
|
229 |
+
]
|
230 |
+
},
|
231 |
+
{
|
232 |
+
"cell_type": "markdown",
|
233 |
+
"id": "ba2ebfb2-85d5-45b9-9fb3-cde897019d7c",
|
234 |
+
"metadata": {},
|
235 |
+
"source": [
|
236 |
+
"### Saving the pages"
|
237 |
+
]
|
238 |
+
},
|
239 |
+
{
|
240 |
+
"cell_type": "code",
|
241 |
+
"execution_count": null,
|
242 |
+
"id": "420321da-016b-4635-acd6-4f816de4e8e1",
|
243 |
+
"metadata": {
|
244 |
+
"scrolled": true
|
245 |
+
},
|
246 |
+
"outputs": [],
|
247 |
+
"source": [
|
248 |
+
"cx_pkl_path = SAVE_PATH / 'complex_data.pkl'\n",
|
249 |
+
"if cx_pkl_path.exists():\n",
|
250 |
+
" with cx_pkl_path.open('rb') as file:\n",
|
251 |
+
" complex_data = pickle.load(file)\n",
|
252 |
+
"else:\n",
|
253 |
+
" complex_data = OrderedDict()\n",
|
254 |
+
" driver = initialize()\n",
|
255 |
+
" for complex in ['Diagnoostestid', 'Tasemeeksami näidistestid']:\n",
|
256 |
+
" complex_data[complex] = OrderedDict()\n",
|
257 |
+
" click_link(driver, complex)\n",
|
258 |
+
" tests_links, tests_names = get_tests(driver)\n",
|
259 |
+
" for test_link, test_name in zip(tests_links, tests_names):\n",
|
260 |
+
" driver.get(test_link)\n",
|
261 |
+
" tests_links2, tests_names2 = find_links_names(driver, 'lugemistest')\n",
|
262 |
+
" for test_link2, test_name2 in zip(tests_links2, tests_names2):\n",
|
263 |
+
" complex_data[complex][test_name2] = OrderedDict()\n",
|
264 |
+
" driver.get(test_link2)\n",
|
265 |
+
" start_test(driver)\n",
|
266 |
+
" save_all_questions(driver, complex_data[complex][test_name2])\n",
|
267 |
+
" finish_test(driver)\n",
|
268 |
+
" links, names, ids = get_test_table(driver)\n",
|
269 |
+
" for url, title, id in zip(links, tqdm(names, desc=test_name2), ids):\n",
|
270 |
+
" driver.get(url)\n",
|
271 |
+
" complex_data[complex][test_name2][title]['html'] = get_page_title_html(driver)[1]\n",
|
272 |
+
" complex_data[complex][test_name2][title]['image'] = get_page_screenshot(driver)\n",
|
273 |
+
" driver.get(HOME_URL)\n",
|
274 |
+
" driver.close()\n",
|
275 |
+
" with cx_pkl_path.open('wb') as file:\n",
|
276 |
+
" pickle.dump(complex_data, file)\n",
|
277 |
+
" image_list = []\n",
|
278 |
+
" for complex in complex_data.keys():\n",
|
279 |
+
" image_list.append(create_image_from_text(complex))\n",
|
280 |
+
" for test_name2 in complex_data[complex].keys():\n",
|
281 |
+
" image_list.append(create_image_from_text(test_name2))\n",
|
282 |
+
" for title in complex_data[complex][test_name2].keys():\n",
|
283 |
+
" image_list.append(complex_data[complex][test_name2][title]['image'])\n",
|
284 |
+
" create_pdf_from_images(image_list, SAVE_PATH / 'complex_data.pdf')"
|
285 |
+
]
|
286 |
+
},
|
287 |
+
{
|
288 |
+
"cell_type": "markdown",
|
289 |
+
"id": "816dec76-0ae8-4845-8488-a1dd7366d4c1",
|
290 |
+
"metadata": {},
|
291 |
+
"source": [
|
292 |
+
"### Extracting the complex data"
|
293 |
+
]
|
294 |
+
},
|
295 |
+
{
|
296 |
+
"cell_type": "code",
|
297 |
+
"execution_count": null,
|
298 |
+
"id": "e90e7309-34b9-473f-a454-bc29c2b37a9e",
|
299 |
+
"metadata": {},
|
300 |
+
"outputs": [],
|
301 |
+
"source": [
|
302 |
+
"driver = get_driver()\n",
|
303 |
+
"body = driver.find_element(By.TAG_NAME, 'body')"
|
304 |
+
]
|
305 |
+
},
|
306 |
+
{
|
307 |
+
"cell_type": "markdown",
|
308 |
+
"id": "89e16f3e-5004-4843-bbad-5ac8d40d86af",
|
309 |
+
"metadata": {},
|
310 |
+
"source": [
|
311 |
+
"#### Helper funcs for Soup"
|
312 |
+
]
|
313 |
+
},
|
314 |
+
{
|
315 |
+
"cell_type": "code",
|
316 |
+
"execution_count": null,
|
317 |
+
"id": "54621a8e-4c2c-49f8-b858-a630573711c3",
|
318 |
+
"metadata": {},
|
319 |
+
"outputs": [],
|
320 |
+
"source": [
|
321 |
+
"def get_text(tag: Tag) -> str:\n",
|
322 |
+
" driver.execute_script('document.body.innerHTML=arguments[0]', str(tag))\n",
|
323 |
+
" return body.text\n",
|
324 |
+
"\n",
|
325 |
+
"def get_elements_between(start_element, end_element):\n",
|
326 |
+
" elements_between = []\n",
|
327 |
+
" current = start_element.next_sibling\n",
|
328 |
+
" while current and current != end_element:\n",
|
329 |
+
" if current.name: # Check if it's a tag and not just text\n",
|
330 |
+
" elements_between.append(current)\n",
|
331 |
+
" current = current.next_sibling\n",
|
332 |
+
" return elements_between\n",
|
333 |
+
"\n",
|
334 |
+
"def smart_get_text(element):\n",
|
335 |
+
" if isinstance(element, Tag) and element.name == 'table':\n",
|
336 |
+
" return process_table(element)\n",
|
337 |
+
" text = get_text(element)\n",
|
338 |
+
" tables = element.find_all('table')\n",
|
339 |
+
" for table in tables:\n",
|
340 |
+
" table_text = process_table(table)\n",
|
341 |
+
" text = text.replace(get_text(table), table_text)\n",
|
342 |
+
" return re.sub(r'\\s*\\n\\s*', '\\n', text).strip()\n",
|
343 |
+
"\n",
|
344 |
+
"def process_table(table):\n",
|
345 |
+
" rows = table.find_all('tr')\n",
|
346 |
+
" if len(rows) == 2:\n",
|
347 |
+
" data = defaultdict(list)\n",
|
348 |
+
" headers = []\n",
|
349 |
+
" for i, row in enumerate(rows):\n",
|
350 |
+
" cells = row.find_all(['th', 'td'])\n",
|
351 |
+
" for j, cell in enumerate(cells):\n",
|
352 |
+
" cell_text = get_text(cell).replace('\\n', ' ')\n",
|
353 |
+
" if i == 0:\n",
|
354 |
+
" headers.append(cell_text)\n",
|
355 |
+
" data[cell_text] = [] # Initialize the list for this header\n",
|
356 |
+
" if i > 0 or len(rows) == 1: # Add data for single-row tables too\n",
|
357 |
+
" data[headers[j]].append(cell_text)\n",
|
358 |
+
" table_strings = []\n",
|
359 |
+
" for header, values in data.items():\n",
|
360 |
+
" table_strings.append(f\"{header}:\\n{', '.join(values)}\")\n",
|
361 |
+
" return '\\n'.join(table_strings)\n",
|
362 |
+
" else:\n",
|
363 |
+
" return get_text(table)\n",
|
364 |
+
"\n",
|
365 |
+
"def get_base_question(soup):\n",
|
366 |
+
" instruction = soup.find('h3').getText(strip=True)\n",
|
367 |
+
" question_div = soup.find('div', {'class': 'ilc_question_Standard'})\n",
|
368 |
+
" middle_elems = get_elements_between(soup.find('h3'), question_div)\n",
|
369 |
+
" middle_context = '\\n\\n'.join([smart_get_text(elem) for elem in middle_elems]) + '\\n\\n'\n",
|
370 |
+
" question = soup.find('div', {'class': 'ilc_qtitle_Title'}).getText(strip=True)\n",
|
371 |
+
" return f'{instruction}\\n\\n{middle_context}{question}'\n",
|
372 |
+
"\n",
|
373 |
+
"def get_opt_an_simple(mqas, soup_answer):\n",
|
374 |
+
" options = [get_text(mqa.parent.parent).strip() for mqa in mqas]\n",
|
375 |
+
" correct_div = soup_answer.find_all('div', {'class': 'ilc_question_SingleChoice'})[1]\n",
|
376 |
+
" imgs = correct_div.find_all('img')\n",
|
377 |
+
" answer = [i+1 for i, img in enumerate(imgs) if img.attrs['title']==\"Kontrollitud\"]\n",
|
378 |
+
" assert len(answer) == 1\n",
|
379 |
+
" answer = int(answer[0])\n",
|
380 |
+
" return options, answer\n",
|
381 |
+
"\n",
|
382 |
+
"def get_opt_an_sel1(select_elem, soup_answer):\n",
|
383 |
+
" options = [elem.getText(strip=True) for elem in select_elem(['option']) if int(elem.attrs['value'])>=0]\n",
|
384 |
+
" correct_div = soup_answer.find_all('div', {'class': 'ilc_question_Standard'})[1]\n",
|
385 |
+
" answer = 1 + options.index(correct_div.find('span', {'class': 'solutionbox'}).getText(strip=True))\n",
|
386 |
+
" assert answer > 0\n",
|
387 |
+
" return options, answer\n",
|
388 |
+
"\n",
|
389 |
+
"def get_opt_an_sel_many(soup, soupan):\n",
|
390 |
+
" question_div = soup.find('div', {'class': 'ilc_question_Standard'})\n",
|
391 |
+
" select_elems = question_div.find_all('select')\n",
|
392 |
+
" if len(soup(['img'])) > 0 or len(question_div(['strong'])) > 0:\n",
|
393 |
+
" return [], [], [] # discard questions with images or mult completions\n",
|
394 |
+
" parents = [elem.parent for elem in select_elems]\n",
|
395 |
+
" select_elems = [elem.extract() for elem in select_elems]\n",
|
396 |
+
" nums_subques = [extract_number_and_text(s.getText(strip=True)) for s in parents]\n",
|
397 |
+
" if any(x is None for x in nums_subques):\n",
|
398 |
+
" return [], [], [] # discard unexpected formats\n",
|
399 |
+
" ans_div = soupan.find_all('div', {'class': 'ilc_question_Standard'})[1]\n",
|
400 |
+
" ans_txt = [ele.getText(strip=True) for ele in ans_div.find_all('span', {'class': 'solutionbox'})]\n",
|
401 |
+
" opts = [[elem.getText(strip=True) for elem in elems(['option']) if int(elem.attrs['value'])>=0] for elems in select_elems]\n",
|
402 |
+
" anss = [1+ops.index(ans) for ans, ops in zip(ans_txt, opts, strict=True)]\n",
|
403 |
+
" assert any(a>0 for a in anss)\n",
|
404 |
+
" if has_repeated(nums_subques):\n",
|
405 |
+
" nums_subques, opts2, opts_expl = sep_num_ABC_mult(parents[0].getText('\\n', strip=True))\n",
|
406 |
+
" assert opts == opts2\n",
|
407 |
+
" return opts_expl, anss, [s[1] for s in nums_subques]\n",
|
408 |
+
" return opts, anss, [s[1] for s in nums_subques]\n",
|
409 |
+
"\n",
|
410 |
+
"def get_complex(ans_html, que_html):\n",
|
411 |
+
" soup = BeautifulSoup(que_html, 'html.parser')\n",
|
412 |
+
" soupan = BeautifulSoup(ans_html, 'html.parser')\n",
|
413 |
+
" question = get_base_question(soup)\n",
|
414 |
+
" select_elems = soup(['select'])\n",
|
415 |
+
" mqas = soup.find_all('input', {'name': 'multiple_choice_result'})\n",
|
416 |
+
" if len(mqas) > 1:\n",
|
417 |
+
" options, answer = get_opt_an_simple(mqas, soupan)\n",
|
418 |
+
" return [question], [options], [answer]\n",
|
419 |
+
" elif len(select_elems) == 1:\n",
|
420 |
+
" options, answer = get_opt_an_sel1(select_elems[0], soupan)\n",
|
421 |
+
" return [question], [options], [answer]\n",
|
422 |
+
" elif len(select_elems) > 1:\n",
|
423 |
+
" optionss, answers, sub_questions = get_opt_an_sel_many(soup, soupan)\n",
|
424 |
+
" if len(answers) > 0:\n",
|
425 |
+
" return [f'{question}\\n\\n{sub}' for sub in sub_questions], optionss, answers\n",
|
426 |
+
" return [], [], []"
|
427 |
+
]
|
428 |
+
},
|
429 |
+
{
|
430 |
+
"cell_type": "markdown",
|
431 |
+
"id": "e6404730-1aec-4ff0-9205-f2fe2f09f295",
|
432 |
+
"metadata": {},
|
433 |
+
"source": [
|
434 |
+
"#### Running"
|
435 |
+
]
|
436 |
+
},
|
437 |
+
{
|
438 |
+
"cell_type": "code",
|
439 |
+
"execution_count": null,
|
440 |
+
"id": "53fe9935-53bd-4cde-b404-33b208d5cd09",
|
441 |
+
"metadata": {},
|
442 |
+
"outputs": [],
|
443 |
+
"source": [
|
444 |
+
"data_list = []\n",
|
445 |
+
"img_list_ok = []\n",
|
446 |
+
"for complex in complex_data.keys():\n",
|
447 |
+
" img_list_ok.append(create_image_from_text(complex))\n",
|
448 |
+
" for test_name2 in complex_data[complex].keys():\n",
|
449 |
+
" img_list_ok.append(create_image_from_text(test_name2))\n",
|
450 |
+
" for title in complex_data[complex][test_name2].keys():\n",
|
451 |
+
" data = complex_data[complex][test_name2][title]\n",
|
452 |
+
" data['category_original_lang'] = test_name2 + ' ' + title\n",
|
453 |
+
" nums = nums_from_title(title)\n",
|
454 |
+
" ques, opts, anss = get_complex(data['html'], data['html_question'])\n",
|
455 |
+
" if len(anss) == 0 or any(len(o) == 1 for op in opts for o in op):\n",
|
456 |
+
" continue\n",
|
457 |
+
" assert len(anss) == len(nums)\n",
|
458 |
+
" img_list_ok.append(data['image'])\n",
|
459 |
+
" for num, que, opt, ans in zip(nums, ques, opts, anss, strict=True):\n",
|
460 |
+
" data_new = data.copy()\n",
|
461 |
+
" data_new['original_question_num'] = num\n",
|
462 |
+
" data_new['question'] = que\n",
|
463 |
+
" data_new['options'] = opt\n",
|
464 |
+
" data_new['answer'] = ans\n",
|
465 |
+
" data_list.append(data_new)\n",
|
466 |
+
"create_pdf_from_images(img_list_ok, SAVE_PATH / 'Diagnoostestid.pdf')"
|
467 |
+
]
|
468 |
+
},
|
469 |
+
{
|
470 |
+
"cell_type": "markdown",
|
471 |
+
"id": "cb929ba0-96d1-4ed7-99f4-bb6bfed04ec2",
|
472 |
+
"metadata": {},
|
473 |
+
"source": [
|
474 |
+
"## Saving the Final JSON"
|
475 |
+
]
|
476 |
+
},
|
477 |
+
{
|
478 |
+
"cell_type": "code",
|
479 |
+
"execution_count": null,
|
480 |
+
"id": "d351f3fa-098d-4679-a62b-a3c65dcf5bb2",
|
481 |
+
"metadata": {},
|
482 |
+
"outputs": [],
|
483 |
+
"source": [
|
484 |
+
"final_list = []\n",
|
485 |
+
"questions = set()\n",
|
486 |
+
"base_model = {\n",
|
487 |
+
" \"language\": \"et\",\n",
|
488 |
+
" \"country\": \"Estonia\",\n",
|
489 |
+
" \"source\": \"https://web.meis.ee/testest/goto.php?target=root_1&client_id=integratsioon\",\n",
|
490 |
+
" \"license\": \"open\",\n",
|
491 |
+
" \"level\": \"Elementary\",\n",
|
492 |
+
" \"category_en\": \"Language Proficiency Test\",\n",
|
493 |
+
" \"category_original_lang\": \"Eesti keele tasemeeksamid\"\n",
|
494 |
+
"}"
|
495 |
+
]
|
496 |
+
},
|
497 |
+
{
|
498 |
+
"cell_type": "code",
|
499 |
+
"execution_count": null,
|
500 |
+
"id": "6c864521-7811-4094-821a-063c8b7cf46d",
|
501 |
+
"metadata": {},
|
502 |
+
"outputs": [],
|
503 |
+
"source": [
|
504 |
+
"base_model['file_name'] = 'Sõeltestid.pdf'\n",
|
505 |
+
"for test_name in simple_data.keys():\n",
|
506 |
+
" for id in simple_data[test_name].keys():\n",
|
507 |
+
" base = base_model.copy()\n",
|
508 |
+
" data = simple_data[test_name][id]\n",
|
509 |
+
" base[\"original_question_num\"] = int(data[\"original_question_num\"])\n",
|
510 |
+
" base[\"question\"] = data[\"question\"]\n",
|
511 |
+
" base[\"options\"] = data[\"options\"]\n",
|
512 |
+
" base[\"answer\"] = str(data[\"answer\"])\n",
|
513 |
+
" if not base['question'] in questions:\n",
|
514 |
+
" final_list.append(base)\n",
|
515 |
+
" questions.add(base['question'])"
|
516 |
+
]
|
517 |
+
},
|
518 |
+
{
|
519 |
+
"cell_type": "code",
|
520 |
+
"execution_count": null,
|
521 |
+
"id": "b989823b-b0a4-4f1f-a007-543e161b5e1a",
|
522 |
+
"metadata": {},
|
523 |
+
"outputs": [],
|
524 |
+
"source": [
|
525 |
+
"base_model['file_name'] = 'Diagnoostestid.pdf'\n",
|
526 |
+
"for data in data_list:\n",
|
527 |
+
" base = base_model.copy()\n",
|
528 |
+
" base[\"original_question_num\"] = int(data[\"original_question_num\"])\n",
|
529 |
+
" base[\"question\"] = data[\"question\"]\n",
|
530 |
+
" base[\"options\"] = data[\"options\"]\n",
|
531 |
+
" base[\"answer\"] = str(data[\"answer\"])\n",
|
532 |
+
" if not base['question'] in questions:\n",
|
533 |
+
" final_list.append(base)\n",
|
534 |
+
" questions.add(base['question'])"
|
535 |
+
]
|
536 |
+
},
|
537 |
+
{
|
538 |
+
"cell_type": "code",
|
539 |
+
"execution_count": null,
|
540 |
+
"id": "aa0eec5b-e4b8-4981-ad5c-f483d84d945e",
|
541 |
+
"metadata": {},
|
542 |
+
"outputs": [],
|
543 |
+
"source": [
|
544 |
+
"with (SAVE_PATH / 'estonian_language_exams.json').open('w') as file:\n",
|
545 |
+
" json.dump(final_list, file)"
|
546 |
+
]
|
547 |
+
},
|
548 |
+
{
|
549 |
+
"cell_type": "code",
|
550 |
+
"execution_count": null,
|
551 |
+
"id": "416e25e7-c15f-40b9-963a-7979f78b82bf",
|
552 |
+
"metadata": {},
|
553 |
+
"outputs": [],
|
554 |
+
"source": []
|
555 |
+
},
|
556 |
+
{
|
557 |
+
"cell_type": "code",
|
558 |
+
"execution_count": null,
|
559 |
+
"id": "491656e7-dfba-4019-9569-f1b32e6d8fa0",
|
560 |
+
"metadata": {},
|
561 |
+
"outputs": [],
|
562 |
+
"source": []
|
563 |
+
},
|
564 |
+
{
|
565 |
+
"cell_type": "code",
|
566 |
+
"execution_count": null,
|
567 |
+
"id": "2ef66610-9c79-4e2e-8369-c5c1bb80aff6",
|
568 |
+
"metadata": {},
|
569 |
+
"outputs": [],
|
570 |
+
"source": []
|
571 |
+
},
|
572 |
+
{
|
573 |
+
"cell_type": "code",
|
574 |
+
"execution_count": null,
|
575 |
+
"id": "f5bcf41f-cd9c-412f-b12f-65e1cda52b9a",
|
576 |
+
"metadata": {
|
577 |
+
"scrolled": true
|
578 |
+
},
|
579 |
+
"outputs": [],
|
580 |
+
"source": [
|
581 |
+
"f1, f2, f3 = [], [], []\n",
|
582 |
+
"for i, data in enumerate(tqdm(data_list)):\n",
|
583 |
+
" soup = BeautifulSoup(data['html_question'], 'html.parser')\n",
|
584 |
+
" soupan = BeautifulSoup(data['html'], 'html.parser')\n",
|
585 |
+
" question = get_base_question(soup)\n",
|
586 |
+
" select_elems = soup(['select'])\n",
|
587 |
+
" mqas = soup.find_all('input', {'name': 'multiple_choice_result'})\n",
|
588 |
+
" if len(mqas) > 1:\n",
|
589 |
+
" options, answer = get_opt_an_simple(mqas, soupan)\n",
|
590 |
+
" f1.append((i, question_div, question, options, answer))\n",
|
591 |
+
" elif len(select_elems) == 1:\n",
|
592 |
+
" options, answer = get_opt_an_sel1(select_elems[0], soupan)\n",
|
593 |
+
" f2.append((i, question, options, answer))\n",
|
594 |
+
" elif len(select_elems) > 1:\n",
|
595 |
+
" options, answers, sub_questions = get_opt_an_sel_many(soup, soupan)\n",
|
596 |
+
" if len(answers) > 0:\n",
|
597 |
+
" f3.append((i, question, options, answers, sub_questions))\n",
|
598 |
+
" else:\n",
|
599 |
+
" continue # return [], [], []"
|
600 |
+
]
|
601 |
+
},
|
602 |
+
{
|
603 |
+
"cell_type": "code",
|
604 |
+
"execution_count": null,
|
605 |
+
"id": "177ca00c-3e0a-4c47-992e-39b2cd96c2be",
|
606 |
+
"metadata": {},
|
607 |
+
"outputs": [],
|
608 |
+
"source": [
|
609 |
+
"f3"
|
610 |
+
]
|
611 |
+
},
|
612 |
+
{
|
613 |
+
"cell_type": "code",
|
614 |
+
"execution_count": null,
|
615 |
+
"id": "ff40db8d-ca10-47e8-b2e8-9a9ac82de7fb",
|
616 |
+
"metadata": {
|
617 |
+
"scrolled": true
|
618 |
+
},
|
619 |
+
"outputs": [],
|
620 |
+
"source": [
|
621 |
+
"t = []\n",
|
622 |
+
"e = []\n",
|
623 |
+
"p = []\n",
|
624 |
+
"p2 = []\n",
|
625 |
+
"n_imgs_strong = 0\n",
|
626 |
+
"for i, data in enumerate(tqdm(data_list)):\n",
|
627 |
+
" soup = BeautifulSoup(data['html_question'], 'html.parser')\n",
|
628 |
+
" soupan = BeautifulSoup(data['html'], 'html.parser')\n",
|
629 |
+
" question_div = soup.find('div', {'class': 'ilc_question_Standard'})\n",
|
630 |
+
" select_elems = question_div.find_all('select')\n",
|
631 |
+
" if len(select_elems) > 1:\n",
|
632 |
+
" \n",
|
633 |
+
" question_div = soup.find('div', {'class': 'ilc_question_Standard'})\n",
|
634 |
+
" select_elems = question_div.find_all('select')\n",
|
635 |
+
" if len(soup(['img'])) > 0 or len(question_div(['strong'])) > 0:\n",
|
636 |
+
" n_imgs_strong += 1\n",
|
637 |
+
" continue\n",
|
638 |
+
" t.append((i, question_div))\n",
|
639 |
+
" parents = [elem.parent for elem in select_elems]\n",
|
640 |
+
" select_elems = [elem.extract() for elem in select_elems]\n",
|
641 |
+
" nums_subques = [extract_number_and_text(s.getText(strip=True)) for s in parents]\n",
|
642 |
+
" if any(x is None for x in nums_subques):\n",
|
643 |
+
" e.append((i, question_div))\n",
|
644 |
+
" continue\n",
|
645 |
+
" ans_div = soupan.find_all('div', {'class': 'ilc_question_Standard'})[1]\n",
|
646 |
+
" ans_txt = [ele.getText(strip=True) for ele in ans_div.find_all('span', {'class': 'solutionbox'})]\n",
|
647 |
+
" opts = [[elem.getText(strip=True) for elem in elems(['option']) if int(elem.attrs['value'])>=0] for elems in select_elems]\n",
|
648 |
+
" anss = [1+ops.index(ans) for ans, ops in zip(ans_txt, opts, strict=True)]\n",
|
649 |
+
" assert any(a>0 for a in anss)\n",
|
650 |
+
" if has_repeated(nums_subques):\n",
|
651 |
+
" nums_subques, opts2, opts_expl = sep_num_ABC_mult(parents[0].getText('\\n', strip=True))\n",
|
652 |
+
" p2.append((i, nums_subques, opts2, opts_expl))\n",
|
653 |
+
" continue\n",
|
654 |
+
" p.append((i, nums_subques, opts))\n",
|
655 |
+
" \n",
|
656 |
+
" # if or len(soup.find_all('input', {'type': 'text'})) > 0:\n",
|
657 |
+
" # continue"
|
658 |
+
]
|
659 |
+
},
|
660 |
+
{
|
661 |
+
"cell_type": "code",
|
662 |
+
"execution_count": null,
|
663 |
+
"id": "65922e51-f4af-4a53-986d-5557786d9cb4",
|
664 |
+
"metadata": {},
|
665 |
+
"outputs": [],
|
666 |
+
"source": [
|
667 |
+
"n_imgs_strong, len(t), len(e), len(p), len(p2)"
|
668 |
+
]
|
669 |
+
},
|
670 |
+
{
|
671 |
+
"cell_type": "code",
|
672 |
+
"execution_count": null,
|
673 |
+
"id": "d1608624-1edf-406c-a9b3-b622fdd23dea",
|
674 |
+
"metadata": {},
|
675 |
+
"outputs": [],
|
676 |
+
"source": [
|
677 |
+
"list1 = [['A', 'B', 'C'], ['A', 'B', 'C']]\n",
|
678 |
+
"list2 = [['A', 'B', 'C'], ['A', 'B', 'C']]\n",
|
679 |
+
"list1 == list2"
|
680 |
+
]
|
681 |
+
},
|
682 |
+
{
|
683 |
+
"cell_type": "code",
|
684 |
+
"execution_count": null,
|
685 |
+
"id": "72188085-06c3-449b-9269-014e70f67dec",
|
686 |
+
"metadata": {},
|
687 |
+
"outputs": [],
|
688 |
+
"source": [
|
689 |
+
"p2"
|
690 |
+
]
|
691 |
+
},
|
692 |
+
{
|
693 |
+
"cell_type": "code",
|
694 |
+
"execution_count": null,
|
695 |
+
"id": "8f7f83c3-8514-407a-a36a-e427ea9aa73d",
|
696 |
+
"metadata": {},
|
697 |
+
"outputs": [],
|
698 |
+
"source": [
|
699 |
+
"p[0], p2[0]"
|
700 |
+
]
|
701 |
+
},
|
702 |
+
{
|
703 |
+
"cell_type": "code",
|
704 |
+
"execution_count": null,
|
705 |
+
"id": "0da63c8a-bbf0-4fbf-920f-d7b6ce435808",
|
706 |
+
"metadata": {},
|
707 |
+
"outputs": [],
|
708 |
+
"source": []
|
709 |
+
},
|
710 |
+
{
|
711 |
+
"cell_type": "code",
|
712 |
+
"execution_count": null,
|
713 |
+
"id": "f282433d-31aa-4f65-8149-9620f841e39c",
|
714 |
+
"metadata": {},
|
715 |
+
"outputs": [],
|
716 |
+
"source": []
|
717 |
+
},
|
718 |
+
{
|
719 |
+
"cell_type": "code",
|
720 |
+
"execution_count": null,
|
721 |
+
"id": "84cb14b2-2de6-4ff2-b062-2e8862ec928a",
|
722 |
+
"metadata": {
|
723 |
+
"scrolled": true
|
724 |
+
},
|
725 |
+
"outputs": [],
|
726 |
+
"source": [
|
727 |
+
"insts"
|
728 |
+
]
|
729 |
+
},
|
730 |
+
{
|
731 |
+
"cell_type": "code",
|
732 |
+
"execution_count": null,
|
733 |
+
"id": "abc34128-b318-4678-ac6d-863f2672039e",
|
734 |
+
"metadata": {},
|
735 |
+
"outputs": [],
|
736 |
+
"source": [
|
737 |
+
"data = data_list[73]\n",
|
738 |
+
"soup = BeautifulSoup(data['html_question'], 'html.parser')\n",
|
739 |
+
"soupan = BeautifulSoup(data['html'], 'html.parser')\n",
|
740 |
+
"question_elems = soup.find_all('div', {'class': 'ilc_qtitle_Title'})\n",
|
741 |
+
"assert len(question_elems) == 1\n",
|
742 |
+
"question = question_elems[0].text.strip()\n",
|
743 |
+
"instruction_elems = soup.find_all('h3')\n",
|
744 |
+
"assert len(instruction_elems) == 1\n",
|
745 |
+
"instruction = instruction_elems[0].text.strip()\n",
|
746 |
+
"middle_context = [extract_text_with_linebreaks(elem) for elem in get_elements_between(instruction_elems[0], soup.find('div', {'class': 'ilc_question_Standard'}))]\n",
|
747 |
+
"select_elems = soup.find_all('select')\n",
|
748 |
+
"mqas = soup.find_all('input', {'name': 'multiple_choice_result'})"
|
749 |
+
]
|
750 |
+
},
|
751 |
+
{
|
752 |
+
"cell_type": "code",
|
753 |
+
"execution_count": null,
|
754 |
+
"id": "2ed0c7d8-928f-4b24-a44c-8d478c245043",
|
755 |
+
"metadata": {},
|
756 |
+
"outputs": [],
|
757 |
+
"source": [
|
758 |
+
"for i, data in enumerate(tqdm(data_list)):\n",
|
759 |
+
" soup = BeautifulSoup(data['html_question'], 'html.parser')\n",
|
760 |
+
" soupan = BeautifulSoup(data['html'], 'html.parser')\n",
|
761 |
+
" select_elems = soup.find_all('select')\n",
|
762 |
+
" if len(select_elems) > 1:\n",
|
763 |
+
" assert len(soup.find_all('div', {'class': 'ilc_qanswer_Answer'}))==1"
|
764 |
+
]
|
765 |
+
},
|
766 |
+
{
|
767 |
+
"cell_type": "code",
|
768 |
+
"execution_count": null,
|
769 |
+
"id": "a7894c02-a608-4c66-b9d9-d6459f7eb8e7",
|
770 |
+
"metadata": {
|
771 |
+
"scrolled": true
|
772 |
+
},
|
773 |
+
"outputs": [],
|
774 |
+
"source": [
|
775 |
+
"soup"
|
776 |
+
]
|
777 |
+
},
|
778 |
+
{
|
779 |
+
"cell_type": "code",
|
780 |
+
"execution_count": null,
|
781 |
+
"id": "55be89c6-538e-454b-aafb-4ab5a4f025a9",
|
782 |
+
"metadata": {
|
783 |
+
"scrolled": true
|
784 |
+
},
|
785 |
+
"outputs": [],
|
786 |
+
"source": [
|
787 |
+
"[sou for sou in soup.find('div', {'class': 'ilc_qanswer_Answer'}).find_all('p') if sou.text.strip()]"
|
788 |
+
]
|
789 |
+
},
|
790 |
+
{
|
791 |
+
"cell_type": "code",
|
792 |
+
"execution_count": null,
|
793 |
+
"id": "f375b430-a2ea-495c-91a8-640f8dd18903",
|
794 |
+
"metadata": {},
|
795 |
+
"outputs": [],
|
796 |
+
"source": [
|
797 |
+
"select_elems"
|
798 |
+
]
|
799 |
+
},
|
800 |
+
{
|
801 |
+
"cell_type": "code",
|
802 |
+
"execution_count": null,
|
803 |
+
"id": "4b0e9638-4cc2-44ba-bf62-141cf60b5b7b",
|
804 |
+
"metadata": {},
|
805 |
+
"outputs": [],
|
806 |
+
"source": [
|
807 |
+
"soup.find_all('input', {'name': 'multiple_choice_result'})[0]"
|
808 |
+
]
|
809 |
+
},
|
810 |
+
{
|
811 |
+
"cell_type": "code",
|
812 |
+
"execution_count": null,
|
813 |
+
"id": "80c056e1-b7c7-4057-aed7-97454a5acfd7",
|
814 |
+
"metadata": {
|
815 |
+
"scrolled": true
|
816 |
+
},
|
817 |
+
"outputs": [],
|
818 |
+
"source": [
|
819 |
+
"test"
|
820 |
+
]
|
821 |
+
},
|
822 |
+
{
|
823 |
+
"cell_type": "code",
|
824 |
+
"execution_count": null,
|
825 |
+
"id": "7420f15f-7b80-4cc8-bf59-389f94f1f4dc",
|
826 |
+
"metadata": {},
|
827 |
+
"outputs": [],
|
828 |
+
"source": [
|
829 |
+
"img"
|
830 |
+
]
|
831 |
+
},
|
832 |
+
{
|
833 |
+
"cell_type": "code",
|
834 |
+
"execution_count": null,
|
835 |
+
"id": "5b47197b-23f3-4e73-8935-0e0c2b97c3ca",
|
836 |
+
"metadata": {},
|
837 |
+
"outputs": [],
|
838 |
+
"source": [
|
839 |
+
"ilc_question_SingleChoice\n",
|
840 |
+
"ilc_qanswer_Answer"
|
841 |
+
]
|
842 |
+
},
|
843 |
+
{
|
844 |
+
"cell_type": "code",
|
845 |
+
"execution_count": null,
|
846 |
+
"id": "dd714667-7de0-462b-94c7-38a6e5e0bdbe",
|
847 |
+
"metadata": {
|
848 |
+
"scrolled": true
|
849 |
+
},
|
850 |
+
"outputs": [],
|
851 |
+
"source": [
|
852 |
+
"soupan"
|
853 |
+
]
|
854 |
+
},
|
855 |
+
{
|
856 |
+
"cell_type": "code",
|
857 |
+
"execution_count": null,
|
858 |
+
"id": "7e68adfe-b6f0-426a-8219-c60ed84bb9da",
|
859 |
+
"metadata": {
|
860 |
+
"scrolled": true
|
861 |
+
},
|
862 |
+
"outputs": [],
|
863 |
+
"source": [
|
864 |
+
"a = get_elements_between(instruction_elems[0], soup.find('div', {'class': 'ilc_question_Standard'}))[0]\n",
|
865 |
+
"print(get_text(a))"
|
866 |
+
]
|
867 |
+
},
|
868 |
+
{
|
869 |
+
"cell_type": "code",
|
870 |
+
"execution_count": null,
|
871 |
+
"id": "38148584-310a-4e22-a4d1-8bff5195c580",
|
872 |
+
"metadata": {
|
873 |
+
"scrolled": true
|
874 |
+
},
|
875 |
+
"outputs": [],
|
876 |
+
"source": [
|
877 |
+
"print('\\n\\n'.join(middle_context))"
|
878 |
+
]
|
879 |
+
},
|
880 |
+
{
|
881 |
+
"cell_type": "code",
|
882 |
+
"execution_count": null,
|
883 |
+
"id": "98359231-78cf-40e3-9c35-b34583cc70d6",
|
884 |
+
"metadata": {},
|
885 |
+
"outputs": [],
|
886 |
+
"source": [
|
887 |
+
"get_elements_between(instruction_elems[0], soup.find('div', {'class': 'ilc_question_Standard'}))"
|
888 |
+
]
|
889 |
+
},
|
890 |
+
{
|
891 |
+
"cell_type": "code",
|
892 |
+
"execution_count": null,
|
893 |
+
"id": "10bcdeab-7c3a-4dc5-90c3-5d7d6c022487",
|
894 |
+
"metadata": {},
|
895 |
+
"outputs": [],
|
896 |
+
"source": []
|
897 |
+
},
|
898 |
+
{
|
899 |
+
"cell_type": "code",
|
900 |
+
"execution_count": null,
|
901 |
+
"id": "70870990-7e81-4b83-bafb-849483f6850f",
|
902 |
+
"metadata": {},
|
903 |
+
"outputs": [],
|
904 |
+
"source": []
|
905 |
+
},
|
906 |
+
{
|
907 |
+
"cell_type": "code",
|
908 |
+
"execution_count": null,
|
909 |
+
"id": "3b5f8892-eb6c-43aa-8357-33c420d090c8",
|
910 |
+
"metadata": {},
|
911 |
+
"outputs": [],
|
912 |
+
"source": [
|
913 |
+
"\n",
|
914 |
+
"from itertools import zip_longest"
|
915 |
+
]
|
916 |
+
},
|
917 |
+
{
|
918 |
+
"cell_type": "code",
|
919 |
+
"execution_count": null,
|
920 |
+
"id": "69b94188-ad77-400c-b2cb-7d4f8b57a08d",
|
921 |
+
"metadata": {},
|
922 |
+
"outputs": [],
|
923 |
+
"source": [
|
924 |
+
"def print_table_column_wise(html_content):\n",
|
925 |
+
" # Parse the HTML content\n",
|
926 |
+
" soup = BeautifulSoup(html_content, 'html.parser')\n",
|
927 |
+
" \n",
|
928 |
+
" # Find the table\n",
|
929 |
+
" table = soup.find('table')\n",
|
930 |
+
" \n",
|
931 |
+
" if not table:\n",
|
932 |
+
" print(\"No table found in the HTML content.\")\n",
|
933 |
+
" return\n",
|
934 |
+
" \n",
|
935 |
+
" # Extract headers\n",
|
936 |
+
" headers = [th.text.strip() for th in table.find_all('th')]\n",
|
937 |
+
" \n",
|
938 |
+
" # Extract rows\n",
|
939 |
+
" rows = []\n",
|
940 |
+
" for tr in table.find_all('tr')[1:]: # Skip the header row\n",
|
941 |
+
" rows.append([td.text.strip() for td in tr.find_all('td')])\n",
|
942 |
+
" \n",
|
943 |
+
" # Transpose the data (swap rows and columns)\n",
|
944 |
+
" columns = list(zip_longest(*rows, fillvalue=''))\n",
|
945 |
+
" \n",
|
946 |
+
" # Print column-wise\n",
|
947 |
+
" for header, column in zip(headers, columns):\n",
|
948 |
+
" print(f\"{header}:\")\n",
|
949 |
+
" for item in column:\n",
|
950 |
+
" print(f\" {item}\")\n",
|
951 |
+
" print() # Empty line between columns\n",
|
952 |
+
"\n",
|
953 |
+
"# Example usage\n",
|
954 |
+
"html_content = \"\"\"\n",
|
955 |
+
"<table>\n",
|
956 |
+
" <tr>\n",
|
957 |
+
" <th>Name</th>\n",
|
958 |
+
" <th>Age</th>\n",
|
959 |
+
" <th>City</th>\n",
|
960 |
+
" </tr>\n",
|
961 |
+
" <tr>\n",
|
962 |
+
" <td>John</td>\n",
|
963 |
+
" <td>30</td>\n",
|
964 |
+
" <td>New York</td>\n",
|
965 |
+
" </tr>\n",
|
966 |
+
" <tr>\n",
|
967 |
+
" <td>Alice</td>\n",
|
968 |
+
" <td>25</td>\n",
|
969 |
+
" <td>Los Angeles</td>\n",
|
970 |
+
" </tr>\n",
|
971 |
+
" <tr>\n",
|
972 |
+
" <td>Bob</td>\n",
|
973 |
+
" <td>35</td>\n",
|
974 |
+
" <td>Chicago</td>\n",
|
975 |
+
" </tr>\n",
|
976 |
+
"</table>\n",
|
977 |
+
"\"\"\"\n",
|
978 |
+
"\n",
|
979 |
+
"print_table_column_wise(html_content)\n"
|
980 |
+
]
|
981 |
+
},
|
982 |
+
{
|
983 |
+
"cell_type": "code",
|
984 |
+
"execution_count": null,
|
985 |
+
"id": "bb95e064-15d7-40e5-bed5-5ec51bf74797",
|
986 |
+
"metadata": {},
|
987 |
+
"outputs": [],
|
988 |
+
"source": [
|
989 |
+
"def extract_text_with_linebreaks(element):\n",
|
990 |
+
" texts = []\n",
|
991 |
+
" for elem in element.descendants:\n",
|
992 |
+
" if isinstance(elem, Comment):\n",
|
993 |
+
" continue # Skip comments\n",
|
994 |
+
" if isinstance(elem, NavigableString):\n",
|
995 |
+
" text = elem.strip()\n",
|
996 |
+
" if text:\n",
|
997 |
+
" texts.append(text)\n",
|
998 |
+
" elif isinstance(elem, Tag):\n",
|
999 |
+
" if elem.name == 'br':\n",
|
1000 |
+
" texts.append('\\n')\n",
|
1001 |
+
" elif elem.name in ['p', 'div', 'tr']:\n",
|
1002 |
+
" if texts and not texts[-1].endswith('\\n'):\n",
|
1003 |
+
" texts.append('\\n')\n",
|
1004 |
+
" return re.sub(r'\\s*\\n\\s*', '\\n', ' '.join(texts)).strip()\n",
|
1005 |
+
" \n",
|
1006 |
+
"\n",
|
1007 |
+
"\n",
|
1008 |
+
"def process_table(table):\n",
|
1009 |
+
" rows = table.find_all('tr')\n",
|
1010 |
+
" if len(rows) <= 2:\n",
|
1011 |
+
" data = defaultdict(list)\n",
|
1012 |
+
" headers = []\n",
|
1013 |
+
" \n",
|
1014 |
+
" for i, row in enumerate(rows):\n",
|
1015 |
+
" cells = row.find_all(['th', 'td'])\n",
|
1016 |
+
" for j, cell in enumerate(cells):\n",
|
1017 |
+
" if i == 0:\n",
|
1018 |
+
" headers.append(cell.get_text(strip=True))\n",
|
1019 |
+
" else:\n",
|
1020 |
+
" data[headers[j]].append(cell.get_text(strip=True))\n",
|
1021 |
+
" \n",
|
1022 |
+
" # Print column-wise\n",
|
1023 |
+
" for header, values in data.items():\n",
|
1024 |
+
" print(f\"{header}: {', '.join(values)}\")\n",
|
1025 |
+
" else:\n",
|
1026 |
+
" print(f\"Table has more than 2 rows ({len(rows)} rows). Not printing.\")"
|
1027 |
+
]
|
1028 |
+
},
|
1029 |
+
{
|
1030 |
+
"cell_type": "code",
|
1031 |
+
"execution_count": null,
|
1032 |
+
"id": "bcfa7414-866c-42e3-ac38-e76414468745",
|
1033 |
+
"metadata": {},
|
1034 |
+
"outputs": [],
|
1035 |
+
"source": [
|
1036 |
+
"soup.find_all('h3')[0].text.strip()"
|
1037 |
+
]
|
1038 |
+
},
|
1039 |
+
{
|
1040 |
+
"cell_type": "code",
|
1041 |
+
"execution_count": null,
|
1042 |
+
"id": "70fdb4ab-1d61-43d6-9299-89ed486e24f5",
|
1043 |
+
"metadata": {},
|
1044 |
+
"outputs": [],
|
1045 |
+
"source": [
|
1046 |
+
"soup.find_all('h3')[0].parent"
|
1047 |
+
]
|
1048 |
+
},
|
1049 |
+
{
|
1050 |
+
"cell_type": "code",
|
1051 |
+
"execution_count": null,
|
1052 |
+
"id": "cf4323fa-8760-459b-a9f5-4a6a95f46301",
|
1053 |
+
"metadata": {},
|
1054 |
+
"outputs": [],
|
1055 |
+
"source": [
|
1056 |
+
"print(extract_text_with_linebreaks(soup.find_all('h3')[0].parent))"
|
1057 |
+
]
|
1058 |
+
},
|
1059 |
+
{
|
1060 |
+
"cell_type": "code",
|
1061 |
+
"execution_count": null,
|
1062 |
+
"id": "49dd98ff-7c7f-4c5e-b827-5201bfc30b72",
|
1063 |
+
"metadata": {},
|
1064 |
+
"outputs": [],
|
1065 |
+
"source": [
|
1066 |
+
"soup.find_all('div', {'class': 'ilc_qtitle_Title'})[0].text.strip()"
|
1067 |
+
]
|
1068 |
+
},
|
1069 |
+
{
|
1070 |
+
"cell_type": "code",
|
1071 |
+
"execution_count": null,
|
1072 |
+
"id": "85a4230f-a1b7-48f0-8b3a-a98c5701e8dd",
|
1073 |
+
"metadata": {},
|
1074 |
+
"outputs": [],
|
1075 |
+
"source": [
|
1076 |
+
"soup.find('div', {'class': 'ilc_question_Standard'})"
|
1077 |
+
]
|
1078 |
+
},
|
1079 |
+
{
|
1080 |
+
"cell_type": "code",
|
1081 |
+
"execution_count": null,
|
1082 |
+
"id": "e1629fef-6747-4184-9379-f08de5312f3d",
|
1083 |
+
"metadata": {},
|
1084 |
+
"outputs": [],
|
1085 |
+
"source": []
|
1086 |
+
},
|
1087 |
+
{
|
1088 |
+
"cell_type": "code",
|
1089 |
+
"execution_count": null,
|
1090 |
+
"id": "a7d60cf2-25b4-47f0-b128-1638f8b14317",
|
1091 |
+
"metadata": {},
|
1092 |
+
"outputs": [],
|
1093 |
+
"source": []
|
1094 |
+
},
|
1095 |
+
{
|
1096 |
+
"cell_type": "code",
|
1097 |
+
"execution_count": null,
|
1098 |
+
"id": "35a63f8a-0e41-4930-93f6-02a5f5171d23",
|
1099 |
+
"metadata": {},
|
1100 |
+
"outputs": [],
|
1101 |
+
"source": [
|
1102 |
+
"extract_numbers_except_first(data_list[0]['title_inner'])"
|
1103 |
+
]
|
1104 |
+
},
|
1105 |
+
{
|
1106 |
+
"cell_type": "code",
|
1107 |
+
"execution_count": null,
|
1108 |
+
"id": "66f0b275-3140-45d6-8800-d30e908be210",
|
1109 |
+
"metadata": {},
|
1110 |
+
"outputs": [],
|
1111 |
+
"source": [
|
1112 |
+
"\n",
|
1113 |
+
"\n",
|
1114 |
+
"# Test cases\n",
|
1115 |
+
"test_cases = [\n",
|
1116 |
+
" 'Lugemistesti 1. ülesanne, 3. küsimus',\n",
|
1117 |
+
" 'Lugemistesti 1. ülesanne, 4. küsimus',\n",
|
1118 |
+
" 'Lugemistesti 2. ülesanne, 6.-10. küsimus',\n",
|
1119 |
+
" 'This is a test 1. with multiple 2. numbers 3. in it.'\n",
|
1120 |
+
"]\n",
|
1121 |
+
"\n",
|
1122 |
+
"for case in test_cases:\n",
|
1123 |
+
" result = extract_numbers(case)\n",
|
1124 |
+
" print(f\"Input: {case}\")\n",
|
1125 |
+
" print(f\"Extracted: {result}\")\n",
|
1126 |
+
" print(get_num_from_title(case))\n",
|
1127 |
+
" print()"
|
1128 |
+
]
|
1129 |
+
},
|
1130 |
+
{
|
1131 |
+
"cell_type": "code",
|
1132 |
+
"execution_count": null,
|
1133 |
+
"id": "da838235-2baa-4a82-a037-98352facfa04",
|
1134 |
+
"metadata": {},
|
1135 |
+
"outputs": [],
|
1136 |
+
"source": []
|
1137 |
+
},
|
1138 |
+
{
|
1139 |
+
"cell_type": "code",
|
1140 |
+
"execution_count": null,
|
1141 |
+
"id": "9e2d22c8-0c6d-4fe7-b1f2-c5e5424ca05a",
|
1142 |
+
"metadata": {},
|
1143 |
+
"outputs": [],
|
1144 |
+
"source": []
|
1145 |
+
},
|
1146 |
+
{
|
1147 |
+
"cell_type": "code",
|
1148 |
+
"execution_count": null,
|
1149 |
+
"id": "8fa603f8-f3dc-4808-a0c7-4042b73c1fc2",
|
1150 |
+
"metadata": {},
|
1151 |
+
"outputs": [],
|
1152 |
+
"source": []
|
1153 |
+
},
|
1154 |
+
{
|
1155 |
+
"cell_type": "code",
|
1156 |
+
"execution_count": null,
|
1157 |
+
"id": "3cbdf775-5fbf-4623-ba7c-1a3c6c7fc105",
|
1158 |
+
"metadata": {},
|
1159 |
+
"outputs": [],
|
1160 |
+
"source": []
|
1161 |
+
},
|
1162 |
+
{
|
1163 |
+
"cell_type": "code",
|
1164 |
+
"execution_count": null,
|
1165 |
+
"id": "9b4c35b5-8cfe-4739-873b-01c31e3d11bc",
|
1166 |
+
"metadata": {},
|
1167 |
+
"outputs": [],
|
1168 |
+
"source": []
|
1169 |
+
},
|
1170 |
+
{
|
1171 |
+
"cell_type": "code",
|
1172 |
+
"execution_count": null,
|
1173 |
+
"id": "6105f143-7cc0-4a0d-85fd-3f4872db4435",
|
1174 |
+
"metadata": {},
|
1175 |
+
"outputs": [],
|
1176 |
+
"source": []
|
1177 |
+
},
|
1178 |
+
{
|
1179 |
+
"cell_type": "code",
|
1180 |
+
"execution_count": null,
|
1181 |
+
"id": "151305db-a520-4434-a0be-648b57ba3030",
|
1182 |
+
"metadata": {},
|
1183 |
+
"outputs": [],
|
1184 |
+
"source": [
|
1185 |
+
"page = driver.find_element(By.ID, 'ilc_Page').get_attribute('innerHTML')\n",
|
1186 |
+
"page_name = driver.find_element(By.CLASS_NAME, 'ilc_page_title_PageTitle').text.strip()\n",
|
1187 |
+
"scraped_data[super_name][test_name2][page_name] = {'page': page}"
|
1188 |
+
]
|
1189 |
+
},
|
1190 |
+
{
|
1191 |
+
"cell_type": "code",
|
1192 |
+
"execution_count": null,
|
1193 |
+
"id": "75df2e3a-b6df-4281-a088-aeab47530068",
|
1194 |
+
"metadata": {},
|
1195 |
+
"outputs": [],
|
1196 |
+
"source": [
|
1197 |
+
"take_full_div_screenshot(driver, 'ilc_Page')"
|
1198 |
+
]
|
1199 |
+
},
|
1200 |
+
{
|
1201 |
+
"cell_type": "code",
|
1202 |
+
"execution_count": null,
|
1203 |
+
"id": "37682c6f-7883-4b7c-bb41-e7b21c041240",
|
1204 |
+
"metadata": {},
|
1205 |
+
"outputs": [],
|
1206 |
+
"source": [
|
1207 |
+
"driver.get_window_size()"
|
1208 |
+
]
|
1209 |
+
},
|
1210 |
+
{
|
1211 |
+
"cell_type": "code",
|
1212 |
+
"execution_count": null,
|
1213 |
+
"id": "b38cd8dc-653e-45b3-b070-13cb01cd7b4b",
|
1214 |
+
"metadata": {},
|
1215 |
+
"outputs": [],
|
1216 |
+
"source": [
|
1217 |
+
"link = driver.find_element(By.PARTIAL_LINK_TEXT, 'Järgmine')\n",
|
1218 |
+
"link.click()"
|
1219 |
+
]
|
1220 |
+
},
|
1221 |
+
{
|
1222 |
+
"cell_type": "code",
|
1223 |
+
"execution_count": null,
|
1224 |
+
"id": "c1f631ef-c212-470a-9b05-7fa6b79e2422",
|
1225 |
+
"metadata": {},
|
1226 |
+
"outputs": [],
|
1227 |
+
"source": [
|
1228 |
+
"try:\n",
|
1229 |
+
" link = driver.find_element(By.PARTIAL_LINK_TEXT, 'Järgmine')\n",
|
1230 |
+
" link.click()\n",
|
1231 |
+
"except NoSuchElementException:\n",
|
1232 |
+
" end_test_questions = True\n",
|
1233 |
+
"\n",
|
1234 |
+
"link = driver.find_element(By.PARTIAL_LINK_TEXT, 'Lõpeta test')\n",
|
1235 |
+
"link.click()\n",
|
1236 |
+
"link = driver.find_element(By.PARTIAL_LINK_TEXT, 'Vaata vastuseid')\n",
|
1237 |
+
"link.click()\n",
|
1238 |
+
"test_link_corrections = driver.current_url"
|
1239 |
+
]
|
1240 |
+
},
|
1241 |
+
{
|
1242 |
+
"cell_type": "code",
|
1243 |
+
"execution_count": null,
|
1244 |
+
"id": "600c21e8-7d78-4115-9313-fe2f202f455d",
|
1245 |
+
"metadata": {},
|
1246 |
+
"outputs": [],
|
1247 |
+
"source": [
|
1248 |
+
"links, names, ids = get_test_table(driver)"
|
1249 |
+
]
|
1250 |
+
},
|
1251 |
+
{
|
1252 |
+
"cell_type": "code",
|
1253 |
+
"execution_count": null,
|
1254 |
+
"id": "4f45a76e-754e-4abb-bf47-71f132365e11",
|
1255 |
+
"metadata": {},
|
1256 |
+
"outputs": [],
|
1257 |
+
"source": [
|
1258 |
+
"driver.get(links[0])"
|
1259 |
+
]
|
1260 |
+
},
|
1261 |
+
{
|
1262 |
+
"cell_type": "code",
|
1263 |
+
"execution_count": null,
|
1264 |
+
"id": "7055833e-a3f4-432d-822a-ef3930c63bef",
|
1265 |
+
"metadata": {},
|
1266 |
+
"outputs": [],
|
1267 |
+
"source": [
|
1268 |
+
"png = driver.get_screenshot_as_png()\n",
|
1269 |
+
"\n",
|
1270 |
+
"# Convert to JPG\n",
|
1271 |
+
"im = Image.open(BytesIO(png))\n",
|
1272 |
+
"im = im.convert('RGB')"
|
1273 |
+
]
|
1274 |
+
},
|
1275 |
+
{
|
1276 |
+
"cell_type": "code",
|
1277 |
+
"execution_count": null,
|
1278 |
+
"id": "78c8bfec-842c-4158-b45b-2b3fa4d91a70",
|
1279 |
+
"metadata": {},
|
1280 |
+
"outputs": [],
|
1281 |
+
"source": [
|
1282 |
+
"driver.set_window_size(1366, 2000)"
|
1283 |
+
]
|
1284 |
+
},
|
1285 |
+
{
|
1286 |
+
"cell_type": "code",
|
1287 |
+
"execution_count": null,
|
1288 |
+
"id": "b3f6a4dc-6d2c-45cf-8497-a9bc779db2c6",
|
1289 |
+
"metadata": {},
|
1290 |
+
"outputs": [],
|
1291 |
+
"source": [
|
1292 |
+
"Image.open(BytesIO(driver.find_element(By.ID, 'ilc_Page').screenshot_as_png))"
|
1293 |
+
]
|
1294 |
+
},
|
1295 |
+
{
|
1296 |
+
"cell_type": "code",
|
1297 |
+
"execution_count": null,
|
1298 |
+
"id": "940283e3-db94-4897-932d-da9fa18c7beb",
|
1299 |
+
"metadata": {},
|
1300 |
+
"outputs": [],
|
1301 |
+
"source": [
|
1302 |
+
"driver.find_element(By.ID, 'ilTopBar')."
|
1303 |
+
]
|
1304 |
+
},
|
1305 |
+
{
|
1306 |
+
"cell_type": "code",
|
1307 |
+
"execution_count": null,
|
1308 |
+
"id": "67080871-b0ac-4c0a-b295-063ff7a53d00",
|
1309 |
+
"metadata": {},
|
1310 |
+
"outputs": [],
|
1311 |
+
"source": [
|
1312 |
+
"div = driver.find_element(By.ID, 'ilc_Page')\n",
|
1313 |
+
"\n",
|
1314 |
+
"# Get the div's size\n",
|
1315 |
+
"div_height = driver.execute_script(\"return arguments[0].offsetHeight;\", div)\n",
|
1316 |
+
"div_width = driver.execute_script(\"return arguments[0].offsetWidth;\", div)"
|
1317 |
+
]
|
1318 |
+
},
|
1319 |
+
{
|
1320 |
+
"cell_type": "code",
|
1321 |
+
"execution_count": null,
|
1322 |
+
"id": "53806954-24f7-4cad-a2e8-fa6327542ec5",
|
1323 |
+
"metadata": {},
|
1324 |
+
"outputs": [],
|
1325 |
+
"source": [
|
1326 |
+
"div_width"
|
1327 |
+
]
|
1328 |
+
},
|
1329 |
+
{
|
1330 |
+
"cell_type": "code",
|
1331 |
+
"execution_count": null,
|
1332 |
+
"id": "63d07115-f13e-412a-bb66-1110485e079d",
|
1333 |
+
"metadata": {},
|
1334 |
+
"outputs": [],
|
1335 |
+
"source": [
|
1336 |
+
"'fixed_content'"
|
1337 |
+
]
|
1338 |
+
},
|
1339 |
+
{
|
1340 |
+
"cell_type": "code",
|
1341 |
+
"execution_count": null,
|
1342 |
+
"id": "ee432a45-5e2e-4af9-9491-7bd66fbd3e29",
|
1343 |
+
"metadata": {},
|
1344 |
+
"outputs": [],
|
1345 |
+
"source": []
|
1346 |
+
},
|
1347 |
+
{
|
1348 |
+
"cell_type": "code",
|
1349 |
+
"execution_count": null,
|
1350 |
+
"id": "6ee6bd6c-8ceb-46ec-84a6-a13bc3fd38b8",
|
1351 |
+
"metadata": {},
|
1352 |
+
"outputs": [],
|
1353 |
+
"source": [
|
1354 |
+
"take_full_div_screenshot(driver, 'ilc_Page')"
|
1355 |
+
]
|
1356 |
+
},
|
1357 |
+
{
|
1358 |
+
"cell_type": "code",
|
1359 |
+
"execution_count": null,
|
1360 |
+
"id": "f8a6f8c0-5f23-4490-a319-71368157af11",
|
1361 |
+
"metadata": {},
|
1362 |
+
"outputs": [],
|
1363 |
+
"source": [
|
1364 |
+
"question_name = driver.find_element(By.CLASS_NAME, 'ilc_page_title_PageTitle').text\n",
|
1365 |
+
"instruction = driver.find_element(By.CLASS_NAME, 'ilc_heading3_Headline3').text\n",
|
1366 |
+
"question = driver.find_element(By.CLASS_NAME, 'ilc_qtitle_Title').text\n",
|
1367 |
+
"table = driver.find_elements(By.CLASS_NAME, 'ilc_table_StandardTable')[-1]\n",
|
1368 |
+
"answers = driver.find_element(By.CLASS_NAME, 'ilc_qinput_ClozeGapSelect')\n",
|
1369 |
+
"answers_options = [elem.text for elem in answers.find_elements(By.TAG_NAME, 'option') if int(elem.get_attribute('value'))>=0]\n",
|
1370 |
+
"scraped_data[test_name2][question_name] = {\n",
|
1371 |
+
" \"category_original_lang\": test_name2,\n",
|
1372 |
+
" \"original_question_num\": int(question_name.split(',')[-1].split('.')[0].strip()),\n",
|
1373 |
+
" \"question\": instruction +'\\n\\n'+ table_to_text(driver, table) + '\\n\\n'+question,\n",
|
1374 |
+
" \"options\": answers_options\n",
|
1375 |
+
"}\n",
|
1376 |
+
"scraped_data[test_name2][question_name]"
|
1377 |
+
]
|
1378 |
+
},
|
1379 |
+
{
|
1380 |
+
"cell_type": "code",
|
1381 |
+
"execution_count": null,
|
1382 |
+
"id": "6a746044-bd60-4215-b822-80cd40269105",
|
1383 |
+
"metadata": {},
|
1384 |
+
"outputs": [],
|
1385 |
+
"source": [
|
1386 |
+
"question_name = driver.find_element(By.CLASS_NAME, 'ilc_page_title_PageTitle').text\n",
|
1387 |
+
"instruction = driver.find_element(By.CLASS_NAME, 'ilc_heading3_Headline3').text\n",
|
1388 |
+
"question = driver.find_element(By.CLASS_NAME, 'ilc_qtitle_Title').text\n",
|
1389 |
+
"text = driver.find_element(By.CSS_SELECTOR, '.ilc_Paragraph.ilc_text_block_List').text.strip()\n",
|
1390 |
+
"example = driver.find_element(By.CSS_SELECTOR, '.ilc_Paragraph.ilc_heading2_Headline2').text.strip()\n",
|
1391 |
+
"garbled_questions = driver.find_element(By.CSS_SELECTOR, '.ilc_qanswer_Answer.ilc_answers.answers.ilAssClozeTest.ilClearFloat')\n",
|
1392 |
+
"paragraphs = [elem for elem in garbled_questions.find_elements(By.TAG_NAME, 'p') if elem.text.strip()]\n",
|
1393 |
+
"q_especifics = [par.text.split('.')[1].split('\\n')[0].strip() for par in paragraphs]\n",
|
1394 |
+
"q_nums = [int(par.text.split('.')[0].strip()) for par in paragraphs]\n",
|
1395 |
+
"q_options = [[elem.text for elem in par.find_elements(By.TAG_NAME, 'option') if int(elem.get_attribute('value'))>=0] for par in paragraphs]\n",
|
1396 |
+
"scraped_data[test_name2][question_name] = [{\n",
|
1397 |
+
" \"category_original_lang\": test_name2,\n",
|
1398 |
+
" \"original_question_num\": q_num,\n",
|
1399 |
+
" \"question\": instruction +'\\n\\n'+ text + '\\n\\n' + example + '\\n\\n' +question + '\\n\\n' + q_esp,\n",
|
1400 |
+
" \"options\": an_opt\n",
|
1401 |
+
"} for q_num, q_esp, an_opt in zip(q_nums, q_especifics, q_options)]\n",
|
1402 |
+
"scraped_data[test_name2][question_name]"
|
1403 |
+
]
|
1404 |
+
},
|
1405 |
+
{
|
1406 |
+
"cell_type": "code",
|
1407 |
+
"execution_count": null,
|
1408 |
+
"id": "dc984165-95df-480e-b79b-528fe196bc2a",
|
1409 |
+
"metadata": {},
|
1410 |
+
"outputs": [],
|
1411 |
+
"source": [
|
1412 |
+
"print(scraped_data[test_name2][question_name]['question'])"
|
1413 |
+
]
|
1414 |
+
},
|
1415 |
+
{
|
1416 |
+
"cell_type": "code",
|
1417 |
+
"execution_count": null,
|
1418 |
+
"id": "318bee09-7682-45f0-b4ab-320cc4eb3464",
|
1419 |
+
"metadata": {},
|
1420 |
+
"outputs": [],
|
1421 |
+
"source": [
|
1422 |
+
"link = driver.find_element(By.PARTIAL_LINK_TEXT, 'Järgmine')\n",
|
1423 |
+
"link.click()"
|
1424 |
+
]
|
1425 |
+
},
|
1426 |
+
{
|
1427 |
+
"cell_type": "code",
|
1428 |
+
"execution_count": null,
|
1429 |
+
"id": "2e00b7c5-2124-49a7-abc4-181cfb21047e",
|
1430 |
+
"metadata": {},
|
1431 |
+
"outputs": [],
|
1432 |
+
"source": [
|
1433 |
+
"answers.find_elements(By.TAG_NAME, 'option')[0].get_attribute('value')"
|
1434 |
+
]
|
1435 |
+
},
|
1436 |
+
{
|
1437 |
+
"cell_type": "code",
|
1438 |
+
"execution_count": null,
|
1439 |
+
"id": "36c0facc-ee97-4555-a6d7-64dfcaf4a775",
|
1440 |
+
"metadata": {},
|
1441 |
+
"outputs": [],
|
1442 |
+
"source": [
|
1443 |
+
"scraped_data[test_name2] = {}"
|
1444 |
+
]
|
1445 |
+
},
|
1446 |
+
{
|
1447 |
+
"cell_type": "code",
|
1448 |
+
"execution_count": null,
|
1449 |
+
"id": "8b307b8c-80ec-46c1-98f8-f5ce8d6ebd42",
|
1450 |
+
"metadata": {},
|
1451 |
+
"outputs": [],
|
1452 |
+
"source": [
|
1453 |
+
"answers_options"
|
1454 |
+
]
|
1455 |
+
},
|
1456 |
+
{
|
1457 |
+
"cell_type": "code",
|
1458 |
+
"execution_count": null,
|
1459 |
+
"id": "6ff29253-b191-4d32-b76e-2771f5df921c",
|
1460 |
+
"metadata": {},
|
1461 |
+
"outputs": [],
|
1462 |
+
"source": []
|
1463 |
+
},
|
1464 |
+
{
|
1465 |
+
"cell_type": "code",
|
1466 |
+
"execution_count": null,
|
1467 |
+
"id": "ab7dd5e3-1c84-46ff-90f6-d6abaca53758",
|
1468 |
+
"metadata": {},
|
1469 |
+
"outputs": [],
|
1470 |
+
"source": [
|
1471 |
+
"print()"
|
1472 |
+
]
|
1473 |
+
},
|
1474 |
+
{
|
1475 |
+
"cell_type": "code",
|
1476 |
+
"execution_count": null,
|
1477 |
+
"id": "b23a335e-5c5e-4798-b471-46496b9e98c0",
|
1478 |
+
"metadata": {},
|
1479 |
+
"outputs": [],
|
1480 |
+
"source": [
|
1481 |
+
"def table_to_text(driver, table):\n",
|
1482 |
+
" # Find all rows in the table\n",
|
1483 |
+
" rows = table.find_elements(By.TAG_NAME, \"tr\")\n",
|
1484 |
+
" \n",
|
1485 |
+
" # Determine the number of columns\n",
|
1486 |
+
" first_row = rows[0]\n",
|
1487 |
+
" num_columns = len(first_row.find_elements(By.XPATH, \"./td|./th\"))\n",
|
1488 |
+
" \n",
|
1489 |
+
" # Initialize a list to store each column's text\n",
|
1490 |
+
" columns_text = [\"\" for _ in range(num_columns)]\n",
|
1491 |
+
" \n",
|
1492 |
+
" # Iterate through each row\n",
|
1493 |
+
" for row in rows:\n",
|
1494 |
+
" cells = row.find_elements(By.XPATH, \"./td|./th\")\n",
|
1495 |
+
" \n",
|
1496 |
+
" # Add each cell's text to the corresponding column's text\n",
|
1497 |
+
" for i, cell in enumerate(cells):\n",
|
1498 |
+
" columns_text[i] += cell.text.strip().replace('\\n', ' ') + ' '\n",
|
1499 |
+
" \n",
|
1500 |
+
" # Join all columns with a separator\n",
|
1501 |
+
" return \"\\n\".join(column.strip() for column in columns_text)\n"
|
1502 |
+
]
|
1503 |
+
},
|
1504 |
+
{
|
1505 |
+
"cell_type": "code",
|
1506 |
+
"execution_count": null,
|
1507 |
+
"id": "3b48091b-56de-45ce-9770-b1b08dd65803",
|
1508 |
+
"metadata": {},
|
1509 |
+
"outputs": [],
|
1510 |
+
"source": [
|
1511 |
+
"print(table_to_text(driver, table))"
|
1512 |
+
]
|
1513 |
+
},
|
1514 |
+
{
|
1515 |
+
"cell_type": "code",
|
1516 |
+
"execution_count": null,
|
1517 |
+
"id": "e28490dd-16f0-4410-ba07-c189b5330cb4",
|
1518 |
+
"metadata": {},
|
1519 |
+
"outputs": [],
|
1520 |
+
"source": [
|
1521 |
+
"instruction.text"
|
1522 |
+
]
|
1523 |
+
},
|
1524 |
+
{
|
1525 |
+
"cell_type": "code",
|
1526 |
+
"execution_count": null,
|
1527 |
+
"id": "41906e50-08fc-487a-bd44-1d303df47a69",
|
1528 |
+
"metadata": {},
|
1529 |
+
"outputs": [],
|
1530 |
+
"source": [
|
1531 |
+
" \n",
|
1532 |
+
" questions_answers = driver.find_elements(By.CLASS_NAME, 'ilc_question_SingleChoice')\n",
|
1533 |
+
" question = questions_answers[0].find_element(By.CLASS_NAME, 'ilc_qtitle_Title')\n",
|
1534 |
+
" answers = questions_answers[1].find_elements(By.CLASS_NAME, 'ilc_qanswer_Answer')\n",
|
1535 |
+
" answers_options = [elem.text for elem in answers]\n",
|
1536 |
+
" correct = [i+1 for i, elem in enumerate(answers) if elem.find_element(By.TAG_NAME, 'img').get_attribute('alt')=='Kontrollitud']\n",
|
1537 |
+
" assert instruction.text\n",
|
1538 |
+
" assert question.text\n",
|
1539 |
+
" assert len(correct) == 1\n",
|
1540 |
+
" scraped_data[test_name][question_id] = {\n",
|
1541 |
+
" \"category_original_lang\": test_name,\n",
|
1542 |
+
" \"original_question_num\": question_id,\n",
|
1543 |
+
" \"question\": instruction.text + '\\n' + question.text,\n",
|
1544 |
+
" \"options\": answers_options,\n",
|
1545 |
+
" \"answer\": str(correct[0])\n",
|
1546 |
+
" }"
|
1547 |
+
]
|
1548 |
+
},
|
1549 |
+
{
|
1550 |
+
"cell_type": "code",
|
1551 |
+
"execution_count": null,
|
1552 |
+
"id": "6eabe86b-d0ac-4de2-811c-f28d930814e8",
|
1553 |
+
"metadata": {},
|
1554 |
+
"outputs": [],
|
1555 |
+
"source": []
|
1556 |
+
},
|
1557 |
+
{
|
1558 |
+
"cell_type": "code",
|
1559 |
+
"execution_count": null,
|
1560 |
+
"id": "c69ef0c0-c8f5-4705-9f05-52cfc1127d12",
|
1561 |
+
"metadata": {},
|
1562 |
+
"outputs": [],
|
1563 |
+
"source": []
|
1564 |
+
},
|
1565 |
+
{
|
1566 |
+
"cell_type": "code",
|
1567 |
+
"execution_count": null,
|
1568 |
+
"id": "08f8323d-9c8e-4170-9da7-d57fb79e3542",
|
1569 |
+
"metadata": {},
|
1570 |
+
"outputs": [],
|
1571 |
+
"source": [
|
1572 |
+
"link = driver.find_element(By.PARTIAL_LINK_TEXT, 'Lõpeta test')\n",
|
1573 |
+
"link.click()\n",
|
1574 |
+
"\n",
|
1575 |
+
"link = driver.find_element(By.PARTIAL_LINK_TEXT, 'Vaata vastuseid')\n",
|
1576 |
+
"link.click()\n",
|
1577 |
+
"test_link_corrections = driver.current_url"
|
1578 |
+
]
|
1579 |
+
},
|
1580 |
+
{
|
1581 |
+
"cell_type": "code",
|
1582 |
+
"execution_count": null,
|
1583 |
+
"id": "1488bb65-3d97-49cc-a01f-9b20753ae16a",
|
1584 |
+
"metadata": {},
|
1585 |
+
"outputs": [],
|
1586 |
+
"source": [
|
1587 |
+
"table = driver.find_element(By.CLASS_NAME, 'table-striped').find_element(By.TAG_NAME, 'tbody')\n",
|
1588 |
+
"questions = table.find_elements(By.TAG_NAME, 'a')\n",
|
1589 |
+
"questions_links = [elem.get_attribute('href') for elem in questions]\n",
|
1590 |
+
"questions_names = [elem.text for elem in questions]\n",
|
1591 |
+
"questions_ids = [int(elem.find_elements(By.TAG_NAME, 'td')[1].text) for elem in table.find_elements(By.TAG_NAME, 'tr')]"
|
1592 |
+
]
|
1593 |
+
},
|
1594 |
+
{
|
1595 |
+
"cell_type": "code",
|
1596 |
+
"execution_count": null,
|
1597 |
+
"id": "44aaadc3-165e-4f56-b5df-968ebb9cc619",
|
1598 |
+
"metadata": {},
|
1599 |
+
"outputs": [],
|
1600 |
+
"source": [
|
1601 |
+
"questions_names"
|
1602 |
+
]
|
1603 |
+
},
|
1604 |
+
{
|
1605 |
+
"cell_type": "code",
|
1606 |
+
"execution_count": null,
|
1607 |
+
"id": "98a36952-6c5d-479f-9646-d222b3c348b9",
|
1608 |
+
"metadata": {},
|
1609 |
+
"outputs": [],
|
1610 |
+
"source": []
|
1611 |
+
},
|
1612 |
+
{
|
1613 |
+
"cell_type": "code",
|
1614 |
+
"execution_count": null,
|
1615 |
+
"id": "0c773666-fee1-4168-b4c7-f09fb78e1788",
|
1616 |
+
"metadata": {},
|
1617 |
+
"outputs": [],
|
1618 |
+
"source": []
|
1619 |
+
},
|
1620 |
+
{
|
1621 |
+
"cell_type": "code",
|
1622 |
+
"execution_count": null,
|
1623 |
+
"id": "2ce11f76-a831-4bd0-acd7-f91d4b0e0889",
|
1624 |
+
"metadata": {},
|
1625 |
+
"outputs": [],
|
1626 |
+
"source": []
|
1627 |
+
},
|
1628 |
+
{
|
1629 |
+
"cell_type": "code",
|
1630 |
+
"execution_count": null,
|
1631 |
+
"id": "0dcc7172-39db-4524-ad0a-7e8ece6bced7",
|
1632 |
+
"metadata": {},
|
1633 |
+
"outputs": [],
|
1634 |
+
"source": []
|
1635 |
+
},
|
1636 |
+
{
|
1637 |
+
"cell_type": "code",
|
1638 |
+
"execution_count": null,
|
1639 |
+
"id": "3ea5d70d-32ce-400a-8551-db5fde0dd56a",
|
1640 |
+
"metadata": {},
|
1641 |
+
"outputs": [],
|
1642 |
+
"source": []
|
1643 |
+
},
|
1644 |
+
{
|
1645 |
+
"cell_type": "code",
|
1646 |
+
"execution_count": null,
|
1647 |
+
"id": "5351ea42-4ef0-48f8-86b5-f27c72021b85",
|
1648 |
+
"metadata": {},
|
1649 |
+
"outputs": [],
|
1650 |
+
"source": [
|
1651 |
+
"driver.quit()"
|
1652 |
+
]
|
1653 |
+
},
|
1654 |
+
{
|
1655 |
+
"cell_type": "code",
|
1656 |
+
"execution_count": null,
|
1657 |
+
"id": "f349626c-c42d-4373-9b53-b3e59ed630af",
|
1658 |
+
"metadata": {},
|
1659 |
+
"outputs": [],
|
1660 |
+
"source": [
|
1661 |
+
"'Eesti keele tasemeeksamid'"
|
1662 |
+
]
|
1663 |
+
},
|
1664 |
+
{
|
1665 |
+
"cell_type": "code",
|
1666 |
+
"execution_count": null,
|
1667 |
+
"id": "057e61ab-a0bd-4a18-a591-60a420a38c33",
|
1668 |
+
"metadata": {},
|
1669 |
+
"outputs": [],
|
1670 |
+
"source": [
|
1671 |
+
"!pip install PyPDF2"
|
1672 |
+
]
|
1673 |
+
}
|
1674 |
+
],
|
1675 |
+
"metadata": {
|
1676 |
+
"kernelspec": {
|
1677 |
+
"display_name": "Python 3 (ipykernel)",
|
1678 |
+
"language": "python",
|
1679 |
+
"name": "python3"
|
1680 |
+
},
|
1681 |
+
"language_info": {
|
1682 |
+
"codemirror_mode": {
|
1683 |
+
"name": "ipython",
|
1684 |
+
"version": 3
|
1685 |
+
},
|
1686 |
+
"file_extension": ".py",
|
1687 |
+
"mimetype": "text/x-python",
|
1688 |
+
"name": "python",
|
1689 |
+
"nbconvert_exporter": "python",
|
1690 |
+
"pygments_lexer": "ipython3",
|
1691 |
+
"version": "3.11.7"
|
1692 |
+
}
|
1693 |
+
},
|
1694 |
+
"nbformat": 4,
|
1695 |
+
"nbformat_minor": 5
|
1696 |
+
}
|
estonian_language_exams/Diagnoostestid.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5f2e70f627cc873ac2a3f4df471618c9bc6943c7b91eea4196f72f7b1a0af388
|
3 |
+
size 10988956
|
estonian_language_exams/Sõeltestid.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:03330bfe985f7ade8de76d1f82c933928d0d41d2628c3b433b698b16ab034ca9
|
3 |
+
size 8449453
|
estonian_language_exams/complex_data.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:dda9a9338a7f47b96a16a93dd0871f26718c7ce2974925834e9a3eb3720fc131
|
3 |
+
size 35285666
|
estonian_language_exams/simple_data.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:56b06626be96cd8028688bb5db68cf4e761f3c13e36550041bd04ee3b33c5d62
|
3 |
+
size 13602797
|
utils_sel.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Utilities Function for dealing with Selenium and generating PDF output"""
|
2 |
+
|
3 |
+
from io import BytesIO
|
4 |
+
from PyPDF2 import PdfMerger
|
5 |
+
from PIL import Image, ImageDraw, ImageFont
|
6 |
+
from selenium import webdriver
|
7 |
+
from selenium.webdriver.common.by import By
|
8 |
+
from selenium.webdriver.chrome.options import Options
|
9 |
+
|
10 |
+
|
11 |
+
def get_driver(production=True, width=1080, height=3840):
|
12 |
+
"""Initializes the driver with a tall headless window in production"""
|
13 |
+
chrome_options = Options()
|
14 |
+
if production:
|
15 |
+
chrome_options.add_argument("--headless")
|
16 |
+
chrome_options.add_argument(f"--window-size={width},{height}")
|
17 |
+
return webdriver.Chrome(options=chrome_options)
|
18 |
+
|
19 |
+
|
20 |
+
def find_links_names(driver, partial_text):
|
21 |
+
"""Return all urls and full names of links that partially have the text"""
|
22 |
+
links = driver.find_elements(By.PARTIAL_LINK_TEXT, partial_text)
|
23 |
+
urls = [elem.get_attribute('href') for elem in links]
|
24 |
+
names = [elem.text for elem in links]
|
25 |
+
return urls, names
|
26 |
+
|
27 |
+
|
28 |
+
def click_button(driver, button_name):
|
29 |
+
"""Click the first button with the name attribute"""
|
30 |
+
button = driver.find_element(By.NAME, button_name)
|
31 |
+
button.click()
|
32 |
+
|
33 |
+
|
34 |
+
def click_link(driver, partial_text):
|
35 |
+
"""Click the first link that partially have the text"""
|
36 |
+
link = driver.find_element(By.PARTIAL_LINK_TEXT, partial_text)
|
37 |
+
link.click()
|
38 |
+
|
39 |
+
|
40 |
+
def get_html_with_images(driver, element):
|
41 |
+
"""Get an string of the innerHTML of the elements with embedded images"""
|
42 |
+
script = """
|
43 |
+
function getBase64Image(img) {
|
44 |
+
var canvas = document.createElement("canvas");
|
45 |
+
canvas.width = img.width;
|
46 |
+
canvas.height = img.height;
|
47 |
+
var ctx = canvas.getContext("2d");
|
48 |
+
ctx.drawImage(img, 0, 0);
|
49 |
+
return canvas.toDataURL("image/png");
|
50 |
+
}
|
51 |
+
var div = arguments[0];
|
52 |
+
var images = div.getElementsByTagName('img');
|
53 |
+
for (var i = 0; i < images.length; i++) {
|
54 |
+
var img = images[i];
|
55 |
+
var src = img.src;
|
56 |
+
if (src.startsWith('http')) {
|
57 |
+
img.src = getBase64Image(img);
|
58 |
+
}
|
59 |
+
}
|
60 |
+
return div.innerHTML;
|
61 |
+
"""
|
62 |
+
return driver.execute_script(script, element)
|
63 |
+
|
64 |
+
|
65 |
+
def bytes_to_image(img_bytes: bytes) -> Image:
|
66 |
+
"""Reads the bytes and returns a PIL.Image"""
|
67 |
+
return Image.open(BytesIO(img_bytes))
|
68 |
+
|
69 |
+
|
70 |
+
def image_to_bytes(img, f_type='JPEG', quality=95):
|
71 |
+
"""Convert a PIL Image to its bytes representation"""
|
72 |
+
bytesio = BytesIO()
|
73 |
+
img.save(bytesio, format=f_type, quality=quality)
|
74 |
+
bytesio.seek(0)
|
75 |
+
return bytesio.getvalue()
|
76 |
+
|
77 |
+
|
78 |
+
def take_full_div_screenshot(driver, div):
|
79 |
+
"""Takes the screenshot of the whole div, uses zoom,
|
80 |
+
resolution may be bad if too big"""
|
81 |
+
div_h = driver.execute_script("return arguments[0].offsetHeight;", div)
|
82 |
+
div_w = driver.execute_script("return arguments[0].offsetWidth;", div)
|
83 |
+
window_height = driver.execute_script("return window.innerHeight;")
|
84 |
+
window_width = driver.execute_script("return window.innerWidth;")
|
85 |
+
zoom_level = min(window_height / div_h, window_width / div_w, 1)
|
86 |
+
driver.execute_script(f"document.body.style.zoom = '{zoom_level}'")
|
87 |
+
driver.execute_script("arguments[0].scrollIntoView();", div)
|
88 |
+
im = bytes_to_image(driver.get_screenshot_as_png())
|
89 |
+
div_x = int(driver.execute_script("return arguments[0].getBoundingClientRect().left;", div))
|
90 |
+
div_y = int(driver.execute_script("return arguments[0].getBoundingClientRect().top;", div))
|
91 |
+
im = im.crop((div_x, div_y, div_x + int(div_w * zoom_level), div_y + int(div_h * zoom_level)))
|
92 |
+
if zoom_level != 1:
|
93 |
+
im = im.resize((div_w, div_h), Image.LANCZOS)
|
94 |
+
driver.execute_script("document.body.style.zoom = '1'")
|
95 |
+
return im
|
96 |
+
|
97 |
+
|
98 |
+
def hide_between_start_end(driver, parent, start: str, end: str, visible=False):
|
99 |
+
"""Hides all children of the parent that are between start (inclusive) to end (text representation)"""
|
100 |
+
children = parent.get_property('children')
|
101 |
+
start_index = None
|
102 |
+
end_index = None
|
103 |
+
for i, child in enumerate(children):
|
104 |
+
if child.text.strip() == start:
|
105 |
+
start_index = i
|
106 |
+
elif child.text.strip() == end:
|
107 |
+
end_index = i
|
108 |
+
break
|
109 |
+
assert start_index is not None
|
110 |
+
assert end_index is not None
|
111 |
+
for i in range(end_index - 1, start_index-1, -1):
|
112 |
+
driver.execute_script(f"arguments[0].style.display = '{'' if visible else 'none'}';\
|
113 |
+
arguments[0].style.visibility = '{'' if visible else 'hidden'}';", children[i])
|
114 |
+
|
115 |
+
|
116 |
+
def copy_element_as_first_child(driver, source_locator, destination_locator):
|
117 |
+
"""Copies the source_locator to be the first child of the destination_locator"""
|
118 |
+
script = """
|
119 |
+
var source = arguments[0];
|
120 |
+
var destination = arguments[1];
|
121 |
+
var clone = source.cloneNode(true);
|
122 |
+
destination.insertBefore(clone, destination.firstChild);
|
123 |
+
"""
|
124 |
+
driver.execute_script(script, source_locator, destination_locator)
|
125 |
+
|
126 |
+
|
127 |
+
def create_pdf_from_images(images_bytes, output_pdf_path, dpi=100, quality=90):
|
128 |
+
"""Create a PDF file with one image per page from the images_bytes list"""
|
129 |
+
pdf_merger = PdfMerger()
|
130 |
+
for img_bytes in images_bytes:
|
131 |
+
img = bytes_to_image(img_bytes)
|
132 |
+
pdf_buffer = BytesIO()
|
133 |
+
img.save(pdf_buffer, format='PDF', resolution=dpi, quality=quality)
|
134 |
+
pdf_buffer.seek(0)
|
135 |
+
pdf_merger.append(pdf_buffer)
|
136 |
+
pdf_merger.write(output_pdf_path)
|
137 |
+
|
138 |
+
|
139 |
+
def create_image_from_text(text, size=(800, 200), as_bytes=True):
|
140 |
+
"""Renders a text"""
|
141 |
+
image = Image.new('RGB', size, color='white')
|
142 |
+
draw = ImageDraw.Draw(image)
|
143 |
+
font = ImageFont.truetype("DejaVuSans.ttf", 28)
|
144 |
+
text_bbox = draw.textbbox((0, 0), text, font=font)
|
145 |
+
text_width = text_bbox[2] - text_bbox[0]
|
146 |
+
text_height = text_bbox[3] - text_bbox[1]
|
147 |
+
position = ((size[0]-text_width)/2, (size[1]-text_height)/2)
|
148 |
+
draw.text(position, text, fill="black", font=font)
|
149 |
+
draw.rectangle([0, 0, size[0]-1, size[1]-1], outline="black")
|
150 |
+
if as_bytes:
|
151 |
+
return image_to_bytes(image)
|
152 |
+
return image
|