Mail.ruПочтаМой МирОдноклассникиВКонтактеИгрыЗнакомстваНовостиКалендарьОблакоЗаметкиВсе проекты

Как можно улучшить этот код?

game hipe HIPE Мастер (1637), на голосовании 6 дней назад
 import requests 
import time
import re
import os
num = 240657845
error_num = 0
while True:
response = requests.get('https://otvet.mail.ru/api/v1/questions/{num}'.format(num=num))
if response.status_code == 200:
a = response.json()
for i in range(1000):
try:
url = a['result']['question']['data']['content'][0]['images'][i]['sizes']['origin']
filename = os.path.basename(url)

# Получаем расширение файла
file_extension = os.path.splitext(filename)[1]
png_name = a['result']['question']['data']["title"]
png_name = re.sub(r'[^a-zA-Z0-9а-яА-ЯёЁ]', '', png_name)
r = requests.get(url)
with open(f'source\\{png_name}{file_extension}','wb') as file:
file.write(r.content)
print('fine!')
error_num = 0
except IndexError: num+=1;break
except KeyError: num+=1;break
print(num)

elif response.status_code == 400:
a = response.json()
if a["error"]["message"] == "question with specified criteria is not found":
print('question with specified criteria is not found')
if error_num == 20: num += 1;error_num = 0;print('find new question');continue
error_num += 1
print(num)
time.sleep(1)
Голосование за лучший ответ
Райан Гослингㅤ Ученик (236) 1 месяц назад
никак хороший код если ты его конечно сам написал
V̲i̲s̲t̲a̲s̲t̲e̲r̲ Искусственный Интеллект (267423) 1 месяц назад
 import requests 
import time
import re
import os

OUTPUT_FOLDER = 'f://results'
IMAGES_FOLDER = os.path.join(OUTPUT_FOLDER, 'f://images')
LOG_FILE = os.path.join(OUTPUT_FOLDER, 'f://log.txt')

num = 240658039
error_num = 0
downloaded_urls = set()
os.makedirs(IMAGES_FOLDER, exist_ok=True)

def log_message(message):
print(message)
with open(LOG_FILE, 'a', encoding='utf-8') as log:
log.write(message + '\n')

def sanitize_filename(filename):
return re.sub(r'[^a-zA-Z0-9а-яА-ЯёЁ]', '', filename)

def download_image(url, filename):
try:
response = requests.get(url, stream=True)
response.raise_for_status()
with open(filename, 'wb') as file:
file.write(response.content)
log_message(f"Скачано: {filename}")
except requests.RequestException as e:
log_message(f"Ошибка при скачивании {url}: {e}")

def process_question(data, question_id):
global downloaded_urls
try:
images = data['result']['question']['data']['content'][0]['images']
title = sanitize_filename(data['result']['question']['data']["title"])
for i, image in enumerate(images):
url = image['sizes']['origin']
if url in downloaded_urls:
log_message(f"Пропуск: изображение уже скачано {url}")
continue

file_extension = os.path.splitext(os.path.basename(url))[1]
filename = os.path.join(IMAGES_FOLDER, f"{title}_{i}{file_extension}")
download_image(url, filename)
downloaded_urls.add(url)
return True
except (IndexError, KeyError) as e:
log_message(f"Ошибка обработки вопроса {question_id}: {e}")
return False

while True:
try:

response = requests.get(f'https://otvet.mail.ru/api/v1/questions/{num}')
if response.status_code == 200:
data = response.json()
if process_question(data, num):
error_num = 0
num += 1
else:
num += 1
elif response.status_code == 400:
data = response.json()
if data.get("error", {}).get("message") == "question with specified criteria is not found":
log_message(f"Вопрос {num} не найден.")
error_num += 1
if error_num >= 20:
num += 1
error_num = 0
log_message("Переходим к следующему вопросу.")
time.sleep(1)
else:
log_message(f"Неожиданный код ответа: {response.status_code}")
num += 1
except requests.RequestException as e:
log_message(f"Ошибка сети: {e}")
time.sleep(5)
Похожие вопросы