MagedSaeed commited on
Commit
01a7381
1 Parent(s): fac718b

Create dataset_scrape.py

Browse files
Files changed (1) hide show
  1. dataset_scrape.py +45 -0
dataset_scrape.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import requests
3
+ import datasets
4
+ import pandas as pd
5
+ from tqdm.auto import tqdm
6
+ from bs4 import BeautifulSoup
7
+
8
+
9
+ BASE_URL = "https://learning.aljazeera.net"
10
+
11
+
12
+ def extract_instruction(page_route, index):
13
+ soup = BeautifulSoup(requests.get(f"{BASE_URL}{page_route}").text, "html.parser")
14
+ question = soup.find("div", {"class": "ask-question"}).text.replace("السؤال :", "")
15
+ answer = soup.find("div", {"class": "ask-answer"}).text.replace("الجواب :", "")
16
+ return {
17
+ "instruction": question.strip(),
18
+ "output": answer.strip(),
19
+ "index": index,
20
+ "instruction_en": "",
21
+ "output_en": "",
22
+ }
23
+
24
+
25
+ index = 9229
26
+ instructions = []
27
+ for page in tqdm(range(93), leave=93):
28
+ time.sleep(2)
29
+ soup = BeautifulSoup(
30
+ requests.get(f"{BASE_URL}/ar/asktheteacher?page={page}", BASE_URL).text,
31
+ "html.parser",
32
+ )
33
+ for href_tag in soup.find_all("a", string="الجواب"):
34
+ href_link = href_tag.get("href")
35
+ try:
36
+ instructions.append(
37
+ extract_instruction(page_route=href_link, index=str(index))
38
+ )
39
+ index += 1
40
+ except Exception as e:
41
+ print("cannot read from page route:", href_link)
42
+
43
+ dataset = datasets.Dataset.from_pandas(pd.DataFrame(data=instructions))
44
+
45
+ dataset.push_to_hub("AskTheTeacherDataset")