Не распаковывает архив
Парсер должен после скачивания должен распаковать архив в папку, если таковой нет - создает, в нужной директории. Но проходит все проверки и ничего не происходит, архив остается в загрузках. Папка есть, но новую не создает и ничего не качает. Прогаю криво, за это прошу прощения.
@staticmethod
def __is_tome_name(chapter: WebElement) -> bool:
td_count = chapter.find_elements(By.TAG_NAME, 'td')
return len(td_count) == 1
@staticmethod
def __is_chapter_disabled(chapter: WebElement) -> bool:
try:
chapter.find_elements(
By.TAG_NAME, 'td'
)[5].find_element(
By.TAG_NAME, 'span'
).get_attribute('class')
except Exception: # noqa
return False
else:
return True
def __check_chapters(self, chapters: Iterable[WebElement]) -> bool:
can_download_any = False
for chapter in chapters:
if not self.__is_tome_name(chapter) and not self.__is_chapter_disabled(chapter):
can_download_any = True
chapter.find_element(By.CSS_SELECTOR, 'input[name="download_chapter[]"]').click()
return can_download_any
def __click_download_btn(self):
try:
self.driver.find_element(By.CSS_SELECTOR, self.daun_lexa_selector).click()
except:
self.driver.find_element(By.CSS_SELECTOR, self.dowload_selector_v2).click()
def __download_chapters(self, title_name: str) -> None:
self.__click_download_btn()
sleep(1)
for file in os.listdir(self.config['download_to_path']):
if file.endswith('zip'):
file_pats = os.path.join(self.config['download_to_path'], file)
print('123',file_pats)
out_path = os.path.join(self.config["unpack_to_path"], title_name)
print('213')
with ZipFile(file_pats, 'r') as zfile:
print('321')
zfile.extractall(out_path)
os.remove(file_pats)
print('removed')
def __parse_page(self, title_name: str):
chapter_table = self.driver.find_element(By.CSS_SELECTOR, self.chapter_table_selector)
chapters = chapter_table.find_elements(By.TAG_NAME, 'tr')
can_download_any = self.__check_chapters(chapters)
self.__download_chapters(title_name) if can_download_any else None
def __parse_link(self, link: str):
self.driver.get(link)
box = Select(self.driver.find_element(By.CSS_SELECTOR, self.selector_box))
box.select_by_visible_text('10')
title_name = self.driver.find_element(
By.CSS_SELECTOR,
self.name_selector,
).text.split('/')[1]
for symbol in self.__ban_symbols:
title_name.replace(symbol, '')
self.driver.find_element(By.CSS_SELECTOR, self.selector_btn).click()
sleep(1)
last_page = self.driver.find_element(
By.CSS_SELECTOR,
self.end_chapter_selector,
).get_attribute('href').split('=')[1]
for page in range(0, int(last_page)):
try:
self.__parse_page(title_name)
except: # noqa
self.driver.get(f'{link}?Chapter_page={page + 1}')
print(f'Ошибка при парсинге {link}?Chapter_page={page}')
sleep(0.5)
sleep(1)