Last active
June 19, 2024 22:10
-
-
Save LucasAlfare/f8efe23a6ecd72639b342ce82e129231 to your computer and use it in GitHub Desktop.
Download de todos os capítulos de One Piece fazendo crawling num site baixando todas as imagens dos capítulos organizando elas em pastas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
from bs4 import BeautifulSoup | |
from concurrent.futures import ThreadPoolExecutor | |
# Base URL dos capítulos | |
base_url = "https://mugiwarasoficial.com/manga/manga-one-piece/capitulo-" | |
# Diretório base onde as imagens serão salvas | |
base_dir = "one_piece_manga" | |
# Função para baixar todas as imagens de um capítulo | |
def download_chapter_images(chapter_number): | |
# URL do capítulo | |
chapter_url = f"{base_url}{chapter_number}/" | |
# Diretório do capítulo | |
chapter_dir = os.path.join(base_dir, f"capitulo_{chapter_number}") | |
os.makedirs(chapter_dir, exist_ok=True) | |
# Fazendo a requisição para obter o HTML | |
response = requests.get(chapter_url) | |
if response.status_code == 200: | |
soup = BeautifulSoup(response.text, 'html.parser') | |
# Encontrar todas as tags <img> com id começando com "image-" | |
images = soup.find_all("img", id=lambda x: x and x.startswith("image-")) | |
for img in images: | |
img_url = img.get("data-src") | |
img_id = img.get("id") | |
# Verificar se a URL da imagem é válida | |
if img_url: | |
print(f"Baixando imagem {img_id} de {img_url}") | |
# Baixar a imagem | |
img_data = requests.get(img_url).content | |
img_filename = os.path.join(chapter_dir, f"{img_id}.jpg") | |
# Salvar a imagem | |
with open(img_filename, 'wb') as img_file: | |
img_file.write(img_data) | |
print(f"Imagem {img_id} do capítulo {chapter_number} baixada e salva.") | |
else: | |
print(f"URL da imagem {img_id} não encontrada (url='{img_url}').") | |
else: | |
print(f"Erro ao acessar o capítulo {chapter_number}.") | |
# Função principal para baixar imagens de vários capítulos | |
def download_all_chapters(start_chapter, end_chapter): | |
for chapter_number in range(start_chapter, end_chapter + 1): | |
print(f"Baixando imagens do capítulo {chapter_number}...") | |
download_chapter_images(chapter_number) | |
print(f"Capítulo {chapter_number} concluído.") | |
# Função principal para gerenciar múltiplas threads | |
def download_all_chapters_in_threads(start_chapter, end_chapter, num_threads=10): | |
chapters_per_thread = (end_chapter - start_chapter + 1) // num_threads | |
futures = [] | |
with ThreadPoolExecutor(max_workers=num_threads) as executor: | |
for i in range(num_threads): | |
thread_start = start_chapter + i * chapters_per_thread | |
thread_end = start_chapter + (i + 1) * chapters_per_thread - 1 | |
if i == num_threads - 1: # Ajustar o último intervalo para pegar todos os capítulos restantes | |
thread_end = end_chapter | |
print(f"Iniciando thread {i+1} para capítulos {thread_start} a {thread_end}.") | |
futures.append(executor.submit(download_all_chapters, thread_start, thread_end)) | |
for future in futures: | |
future.result() # Aguarda cada thread terminar | |
# para começar o download de TUDO, basta chamar a função `download_all_chapters_in_threads()` | |
# passando o primeiro e o capítulo que vc quer. Ex.: `download_all_chapters_in_threads(1, 1000)` | |
# é possível que um ou outro capítulo fique sem ser baixado. Se acontecer, veja qual faltou e | |
# baixe manualmente usando a função `download_chapter_images(NUM_DO_CAP_AQUI)`. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment