/
VolcanoByte
/
vbBooks
Обзор
Документация
Войти
/
VolcanoByte
/
vbBooks
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
experiments/DataExtract/bs4Extract.py
62 строки
2 KB
Eugene Gavrilyuk
test: Эксперименты
06 авг 2025, 16:33
06 авг 2025, 16:33
c5e5274
Код
Авторство
О чём код?
""" Использование BeautifulSoup """ import os import sys from bs4 import BeautifulSoup from tqdm import tqdm import logging HEADER = '{http://www.gribuser.ru/xml/fictionbook/2.0}' tags = dict() tagCount = dict() logging.basicConfig(level=logging.INFO, filename="books.log",filemode="w") def getSoup(filename: str) -> BeautifulSoup: f1 = open(filename, 'r', encoding='windows-1251') try: return BeautifulSoup(f1, "xml") except: f2 = open(filename, 'r', encoding='utf-8') try: return BeautifulSoup(f2, "xml") except Exception as e: print(filename, e) finally: f2.close() finally: f1.close() def getBookInfo(filename: str): """Начинаем обходить все элементы в title-info""" soup = getSoup(filename) if soup: desc = soup.find('title-info') genres = desc.find_all('genre') title = desc.find('book-title') authors = desc.find_all('author') logging.info(filename) for author in authors: first_name = author.find('first-name') print(first_name.text) def processFolder(folder: str): """Обходим все файлы в папке""" files = os.listdir(folder) for filename in tqdm(files): getBookInfo(os.path.join(folder, filename)) if len(sys.argv) <= 1: print('Usage: extract <path>') exit() processFolder(sys.argv[1]) # processFolder('fb2-061748-062680') # processFolder('fb2-089725-092162') # processFolder('fb2-112730-113380') for key, value in tags.items(): print(f'{tagCount[key]: >2}: [{key}] in file {value}')