File size: 2,072 Bytes
3d6500b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import re
import html
from urllib.request import urlopen

def download_file(url, filename):
    if not os.path.exists(filename):
        with urlopen(url) as response:
            html_content = response.read().decode('ISO-8859-1')
            with open(filename, 'w', encoding='utf-8') as out_file:
                out_file.write(html_content)

def strip_html_and_markers(filename):
    with open(filename, 'r', encoding='utf-8') as file:
        content = file.read()
        content = re.sub(r'<head>.*?</head>', '', content, flags=re.DOTALL)
        # Remove anchor tags (both upper and lower case) and their content
        content = re.sub(r'<a href=[^>]+>.*?</a>', '', content, flags=re.IGNORECASE | re.DOTALL)
        # Remove other HTML tags
        content = re.sub(r'<[^>]+>', '', content)
        # Remove markers like [1] or [et]
        content = re.sub(r'\[.*?\]', '', content)
        # Remove empty brackets
        content = re.sub(r'\[\]', '', content)

        content = re.sub(r'\n[\s]*\n', '\n\n', content)
        content = re.sub(r'^[ \t]+', '\t', content, flags=re.MULTILINE)

        return html.unescape(content)

def main():
    urls = [
        'http://www.thelatinlibrary.com/cicero/fin1.shtml',
        'http://www.thelatinlibrary.com/cicero/fin2.shtml',
        'http://www.thelatinlibrary.com/cicero/fin3.shtml',
        'http://www.thelatinlibrary.com/cicero/fin4.shtml',
        'http://www.thelatinlibrary.com/cicero/fin5.shtml',
    ]

    filenames = [
        'fin1.shtml',
        'fin2.shtml',
        'fin3.shtml',
        'fin4.shtml',
        'fin5.shtml',
    ]
    target_filenames = [
        'fin1.txt',
        'fin2.txt',
        'fin3.txt',
        'fin4.txt',
        'fin5.txt',
    ]

    for url, filename, target_filename in zip(urls, filenames, target_filenames):
        download_file(url, filename)
        final_text = strip_html_and_markers(filename) + '\n'

        with open(target_filename, 'w', encoding='utf-8') as file:
            file.write(final_text)

if __name__ == "__main__":
    main()