-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_fables.py
91 lines (72 loc) · 2.1 KB
/
get_fables.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3.7
import os
from pathlib import Path
from bs4 import BeautifulSoup
"""
txt:
ligne 1 = livre
ligne 2 = titre
ligne 3 et + = fable
<p class="fable">
La Cigale, ayant chanté<br />
Tout l'été,<br />
Se trouva fort dépourvue<br />
...
- Vous chantiez ? j'en suis fort aise.<br />
Eh bien! dansez maintenant.
</p>
"""
def get_all_files_list(directory, extentions):
"""
Lit le dossier et tous les sous-dosssiers.
Retourne la liste de tous les fichiers avec les extentions de
la liste extentions.
"""
file_list = []
for path, subdirs, files in os.walk(directory):
for name in files:
for extention in extentions:
if name.endswith(extention):
file_list.append(str(Path(path, name)))
return file_list
def write_data_in_file(data, fichier, mode="w"):
with open(fichier, mode) as fd:
fd.write(data)
fd.close()
def read_file(file_name):
"""
Retourne les datas lues dans le fichier avec son chemin/nom
Retourne None si fichier inexistant ou impossible à lire .
"""
try:
with open(file_name) as f:
data = f.read()
f.close()
except:
data = None
print("Fichier inexistant ou impossible à lire:", file_name)
return data
def get_infos(html):
"""html = mesfables/livre-1/11-l-homme-et-son-image.html"""
soup = BeautifulSoup(read_file(html), "lxml")
fable = soup.find_all('p', class_='fable')[0].get_text()
title = soup.title.get_text()
livre = html.split('/')[1]
return title, fable, livre
def save_in_txt(title, fable, livre):
"""
ligne 1 = livre
ligne 2 = titre
ligne 3 et + = fable
"""
data = livre + '\n' + title + '\n' + fable
fichier = './fables_txt/' + title + '.txt'
write_data_in_file(data, fichier)
def main():
htmls = get_all_files_list("./mesfables", [".html"])
for html in htmls:
title, fable, livre = get_infos(html)
save_in_txt(title, fable, livre)
print('Done.')
if __name__ == "__main__":
main()