-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharticle_crawler.py
73 lines (58 loc) · 2.65 KB
/
article_crawler.py
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
import multiprocessing
import re
import requests
import justext
import random
import os
from multiprocessing import Manager
from itertools import repeat
# CONFIG
TXT_FILES_PATH = 'your-directory-path/' # remember about the '/', has to be created before starting the script
TXT_URLS = 'txt-file-with-urls' # provide directory to .txt containing URLs
MIN_LENGTH = 2000 # minimal number of characters for a text to be collected
PROCESSES = 3 # number of processes, from 1 to os.cpu_count()
###
def process_item(item, counter, total_files):
counter.value += 1
ccc = random.randrange(1, 1000000000)
txt = ''
response = requests.get(item)
"""
Tool used to analyze and extract text from websites is a library called 'JusText' by Jan Pomikalek.
Be sure to experiment with different URLs from set manually and fine-tune the params provided by the author.
Ones provided below are the results of experiments on a set of URLs from a forum type website. Most likely they
will not fit to your use-case.
Fine-tuning should be done once per website/dataset.
To try different possibilites and sets of params, visit:
https://nlp.fi.muni.cz/projects/justext/
"""
paragraphs = justext.justext(response.content, justext.get_stoplist("Polish"),
max_heading_distance=150,
length_low=10,
length_high=100,
stopwords_low=0.1,
stopwords_high=0.2,
max_link_density=0.2)
for paragraph in paragraphs:
if not paragraph.is_boilerplate:
txt += paragraph.text + " "
txt = re.sub(r"\n{2,}", "\n", txt)
print("Processing file: " + item, counter.value, "/", total_files, round((counter.value/total_files*100), 2), "%")
if len(txt) > MIN_LENGTH:
try:
with open(f"{TXT_FILES_PATH}" + str(ccc) + ".txt", 'w', encoding="utf-8") as f:
f.write(txt)
except Exception as e:
print(e)
if __name__ == '__main__':
manager = Manager()
counter = manager.Value('i', 0)
with open(TXT_URLS, "r", encoding="utf-8") as f:
txt_files = f.read().split("\n")
total_files = len(txt_files)
with multiprocessing.Pool(processes=PROCESSES) as pool:
results = pool.starmap(process_item,
zip(txt_files,
repeat(counter),
repeat(total_files)),
chunksize=8)