File size: 1,480 Bytes
01c11f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
from tqdm import tqdm
from bs4 import BeautifulSoup as BS
from xtractor.utils import dumps_the_json, jsonl_converter

def get_hrefs():
    all_hrefs = []
    for i in range(1, 11):
        # url = f"https://www.muftiselangor.gov.my/keputusan-fatwa-tidak-diwartakan/{i}"
        url  = f"https://www.muftiselangor.gov.my/keputusan-fatwa-diwartakan/{i}"
        response = requests.get(url)
        print(url, response.status_code)
        bs = BS(response.text, "lxml")
        div_all = bs.find_all("div", {"class":"elementor-post__card"})
        hrefs = [x.find("a", href=True)["href"] for x in div_all]
        all_hrefs.append(hrefs)

    all_hrefs_ = [y for x in all_hrefs for y in x]
    return all_hrefs_

def get_title_body(all_hrefs_):
    full_data = {"title":[], "body":[]}
    for link in tqdm(all_hrefs_):
        title = " ".join(link.split("/")[-2].split("-")).title()
        response = requests.get(link)
        bs = BS(response.text, "lxml")
    
        main = bs.find("main", class_="site-main")
        body = main.text.strip()
    
        full_data["title"].append(title)
        full_data["body"].append(body)
        
    return full_data


if __name__ == "__main__":
    all_hrefs = get_hrefs()
    full_data = get_title_body()
    dumps_the_json(content=full_data, json_file_name="fatwa_selangor_diwartakan.json")
    jsonl_converter("fatwa_selangor_diwartakan.json","fatwa_selangor_diwartakan.jsonl", 
                "title", "body")