Ammar Azman commited on
Commit
f5bc78e
1 Parent(s): 65a196e

Create scraper.py

Browse files
Files changed (1) hide show
  1. scraper.py +56 -0
scraper.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from bs4 import BeautifulSoup as BS
3
+ from tqdm import tqdm
4
+
5
+ # !git clone https://github.com/Ammar-Azman/xtractor.git
6
+
7
+ from xtractor.utils import dumps_the_json, jsonl_converter
8
+
9
+ def get_lyrics_link():
10
+ links = []
11
+ for i in tqdm(range(1, 199)):
12
+ url= f"http://liriklagu.net/melayu/page/{i}"
13
+ response = requests.get(url)
14
+ # print(url, response.status_code)
15
+ bs = BS(response.text, "lxml")
16
+ all_h2 = bs.find_all("h2", class_="post-title")
17
+ for h2 in all_h2:
18
+ href = h2.find("a", href=True)["href"]
19
+ links.append(href)
20
+ return links
21
+
22
+ def get_data(links:list, start, end):
23
+ full_data = {"title":[], "body":[]}
24
+
25
+ for link in tqdm(links[start:end]):
26
+ try:
27
+ response = requests.get(link)
28
+ tajuk = link.split("/")[-2].replace("-", " ").title()
29
+ penyanyi = link.split("/")[-3].replace("-", " ").title()
30
+ title = f"{tajuk} - {penyanyi}"
31
+ # print(response.status_code)
32
+ bs = BS((response.text).replace("<br>", "\n"), "lxml")
33
+ div = bs.find("div", class_="post-content entry-content")
34
+ p_tag = div.find_all("p")
35
+
36
+ lyrics = []
37
+ for p in p_tag:
38
+ text = p.text
39
+ lyrics.append(text)
40
+
41
+ full_text = " ".join(lyrics)
42
+ full_data["title"].append(title)
43
+ full_data["body"].append(full_text)
44
+ except:
45
+ print("Error", link)
46
+ continue
47
+
48
+ return full_data
49
+
50
+ if __name__ == "__main__":
51
+ links = get_lyrics_link()
52
+ full_data = get_data(links, 0, None) # index 0 till end
53
+ dumps_the_json(content=full_data, json_file_name="lirik_lagu_dotnet_0_end.json")
54
+ jsonl_converter("lirik_lagu_dotnet_0_end.json","lirik_lagu_dotnet_0_end.jsonl",
55
+ "title", "body")
56
+