-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathck3_crawler.py
More file actions
204 lines (149 loc) · 4.98 KB
/
ck3_crawler.py
File metadata and controls
204 lines (149 loc) · 4.98 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from curl_cffi import requests
from bs4 import BeautifulSoup
from queue import Queue, Empty
from threading import Thread, Lock
import time
import json
API_URL = "https://ck3.paradoxwikis.com/api.php"
START_PAGE = "Crusader_Kings_III_Wiki"
OUTPUT_FILE = "ck3_database.txt"
NUM_WORKERS = 8
REQUEST_DELAY = 0.15
MAX_PAGES_TO_SCRAPE = None
IMPERSONATE = "chrome120"
visited_pages = set()
queued_pages = {START_PAGE}
visit_lock = Lock()
queue_lock = Lock()
count_lock = Lock()
pages_scraped = 0
stop_crawl = False
pages_queue = Queue()
write_queue = Queue()
pages_queue.put(START_PAGE)
def make_session():
return requests.Session(
impersonate=IMPERSONATE,
timeout=15,
)
def get_page_content(session, title):
params = {
"action": "parse",
"page": title,
"format": "json",
"prop": "text|links",
"redirects": 1,
}
try:
response = session.get(API_URL, params=params)
data = response.json()
if "error" in data:
print(f"Skipped '{title}': {data['error'].get('info')}")
return None
return data["parse"]
except Exception as e:
print(f"Connection failed for '{title}': {e}")
return None
def clean_html_to_text(html_code):
soup = BeautifulSoup(html_code, "html.parser")
for tag in soup(["script", "style"]):
tag.extract()
text = soup.get_text(separator="\n")
lines = (line.strip() for line in text.splitlines())
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
text = "\n".join(chunk for chunk in chunks if chunk)
return text
def enqueue_link(link_title):
global stop_crawl
with queue_lock:
if stop_crawl:
return False
if link_title in visited_pages or link_title in queued_pages:
return False
if MAX_PAGES_TO_SCRAPE is not None:
total_known = len(visited_pages) + len(queued_pages)
if total_known >= MAX_PAGES_TO_SCRAPE:
return False
queued_pages.add(link_title)
pages_queue.put(link_title)
return True
def worker(worker_id):
global pages_scraped, stop_crawl
session = make_session()
while True:
if stop_crawl and pages_queue.empty():
return
try:
current_title = pages_queue.get(timeout=1)
except Empty:
if stop_crawl:
return
continue
with visit_lock:
if current_title in visited_pages:
pages_queue.task_done()
continue
visited_pages.add(current_title)
with count_lock:
if MAX_PAGES_TO_SCRAPE is not None and pages_scraped >= MAX_PAGES_TO_SCRAPE:
stop_crawl = True
pages_queue.task_done()
continue
print(f"[Worker {worker_id}] Scraping: {current_title} ...", end="", flush=True)
data = get_page_content(session, current_title)
if data:
raw_html = data["text"]["*"]
readable_text = clean_html_to_text(raw_html)
write_queue.put({
"title": current_title,
"text": readable_text,
})
new_links_count = 0
if "links" in data:
for link_obj in data["links"]:
link_title = link_obj["*"]
namespace = link_obj.get("ns", 0)
if namespace == 0:
if enqueue_link(link_title):
new_links_count += 1
with count_lock:
pages_scraped += 1
scraped_so_far = pages_scraped
if MAX_PAGES_TO_SCRAPE is not None and pages_scraped >= MAX_PAGES_TO_SCRAPE:
stop_crawl = True
print(f" Done. (Found {new_links_count} new pages, total scraped: {scraped_so_far})")
else:
print(" Failed.")
pages_queue.task_done()
time.sleep(REQUEST_DELAY)
def writer():
separator = "=" * 60
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
while True:
item = write_queue.get()
if item is None:
write_queue.task_done()
return
f.write(f"\n\n{separator}\nPAGE TITLE: {item['title']}\n{separator}\n")
f.write(item["text"])
f.write(f"\n{separator}\n")
f.flush()
write_queue.task_done()
print(f"Starting concurrent crawler on: {START_PAGE}")
print(f"Saving data to: {OUTPUT_FILE}")
print(f"Workers: {NUM_WORKERS}")
writer_thread = Thread(target=writer, daemon=True)
writer_thread.start()
workers = []
for i in range(NUM_WORKERS):
t = Thread(target=worker, args=(i + 1,), daemon=True)
t.start()
workers.append(t)
pages_queue.join()
stop_crawl = True
for t in workers:
t.join(timeout=2)
write_queue.put(None)
write_queue.join()
writer_thread.join(timeout=2)
print(f"Crawler finished! Scraped {pages_scraped} pages.")