Разобрать каждый файл в папке с помощью BeautifulSoup / Python, сохранить как новый файл - PullRequest
0 голосов
/ 07 января 2020

У меня есть скрипт python:

#!/usr/bin/env python3

#########################################################################################################################

from sys     import argv
from pathlib import Path
from bs4     import BeautifulSoup

#########################################################################################################################

def extractor(html):

    soup   = BeautifulSoup(html,'lxml')
    for junk in soup(["a", "button", "footer", "form", "header", "h3", "label", "nav", "span", "script", "style", "ul"]):
        junk.extract()

    text   = soup.get_text()
    lines  = (line.strip() for line in text.splitlines())
    chunks = (phrase.strip() for line in lines for phrase in line.split("  "))
    output = '\n\n'.join(chunk for chunk in chunks if chunk)

    return(output)

#########################################################################################################################

def main(x):

    try:
        with open(str(x), encoding='utf8') as f:
            html     = f.read()
            x.with_suffix('.txt').write_text(extractor(html))

    except (IsADirectoryError,PermissionError):
        filenames    = Path(x).glob('**/*.html')
        for target in filenames:

            with open(str(target), encoding='utf8') as f:
                html = f.read()
                target.with_suffix('.txt').write_text(extractor(html))

#########################################################################################################################

if __name__ == "__main__":
    main(Path(argv[-1]))

#########################################################################################################################

Когда я его запускаю, он анализирует только 1-8 html файлов во всей папке. как я могу получить его для анализа всей папки ??

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...