File size: 1,354 Bytes
01a7381
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time
import requests
import datasets
import pandas as pd
from tqdm.auto import tqdm
from bs4 import BeautifulSoup


BASE_URL = "https://learning.aljazeera.net"


def extract_instruction(page_route, index):
    soup = BeautifulSoup(requests.get(f"{BASE_URL}{page_route}").text, "html.parser")
    question = soup.find("div", {"class": "ask-question"}).text.replace("السؤال :", "")
    answer = soup.find("div", {"class": "ask-answer"}).text.replace("الجواب :", "")
    return {
        "instruction": question.strip(),
        "output": answer.strip(),
        "index": index,
        "instruction_en": "",
        "output_en": "",
    }


index = 9229
instructions = []
for page in tqdm(range(93), leave=93):
    time.sleep(2)
    soup = BeautifulSoup(
        requests.get(f"{BASE_URL}/ar/asktheteacher?page={page}", BASE_URL).text,
        "html.parser",
    )
    for href_tag in soup.find_all("a", string="الجواب"):
        href_link = href_tag.get("href")
        try:
            instructions.append(
                extract_instruction(page_route=href_link, index=str(index))
            )
            index += 1
        except Exception as e:
            print("cannot read from page route:", href_link)

dataset = datasets.Dataset.from_pandas(pd.DataFrame(data=instructions))

dataset.push_to_hub("AskTheTeacherDataset")