Datasets:
release scraper code
Browse files
main.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from datasets import Dataset
|
3 |
+
from selectolax.lexbor import LexborHTMLParser
|
4 |
+
|
5 |
+
# How many pages to seek for article recommendations?
|
6 |
+
# (https://www.storm.mg/articles/{page_id})
|
7 |
+
N_PAGES_OF_ARTICLES_RECOMMENDATIONS = 100
|
8 |
+
|
9 |
+
base_url = "https://www.storm.mg/articles/%i"
|
10 |
+
user_agent = (
|
11 |
+
# use mine, or put your user agent here
|
12 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
|
13 |
+
"Chrome/121.0.0.0 Safari/537.36 OPR/107.0.0.0"
|
14 |
+
)
|
15 |
+
|
16 |
+
def read_article(link: str):
|
17 |
+
"""Read an article on www.storm.mg."""
|
18 |
+
r = requests.get(link, headers={ "User-Agent": user_agent })
|
19 |
+
r.raise_for_status()
|
20 |
+
|
21 |
+
contents = []
|
22 |
+
parser = LexborHTMLParser(r.text)
|
23 |
+
|
24 |
+
for paragraph in parser.css("p[aid]"):
|
25 |
+
contents.append(paragraph.text(separator=" ", strip=True))
|
26 |
+
|
27 |
+
return contents
|
28 |
+
|
29 |
+
|
30 |
+
def generate_dataset():
|
31 |
+
"""Generate the dataset."""
|
32 |
+
for page_id in range(N_PAGES_OF_ARTICLES_RECOMMENDATIONS):
|
33 |
+
r = requests.get(base_url % (page_id + 1), headers={
|
34 |
+
"User-Agent": user_agent
|
35 |
+
})
|
36 |
+
r.raise_for_status()
|
37 |
+
|
38 |
+
parser = LexborHTMLParser(r.text)
|
39 |
+
articles = parser.css(".category_cards_wrapper .category_card.card_thumbs_left")
|
40 |
+
|
41 |
+
for article in articles:
|
42 |
+
image = article.css_first("img").attributes['src']
|
43 |
+
title = article.css_first(".card_title").text()
|
44 |
+
tag = article.css_first(".tags_wrapper a").text()
|
45 |
+
|
46 |
+
info = article.css_first("p.card_info.right")
|
47 |
+
author = info.css_first(".info_author").text()
|
48 |
+
timestamp = info.css_first(".info_time").text()
|
49 |
+
link = article.css_first(".link_title").attributes['href']
|
50 |
+
|
51 |
+
yield {
|
52 |
+
"image": image,
|
53 |
+
"title": title,
|
54 |
+
"content": "\n".join(read_article(link)),
|
55 |
+
"tag": tag,
|
56 |
+
"author": author,
|
57 |
+
"timestamp": timestamp,
|
58 |
+
"link": link
|
59 |
+
}
|
60 |
+
|
61 |
+
dataset = Dataset.from_generator(generate_dataset)
|
62 |
+
dataset.save_to_disk(
|
63 |
+
f"storm-org-articles-{20 * N_PAGES_OF_ARTICLES_RECOMMENDATIONS}"
|
64 |
+
)
|