stanis-tits-scrap/stanis-tits.py

100 lines
3.2 KiB
Python
Raw Normal View History

2016-10-28 00:29:36 +03:00
#!/usr/bin/env python3
2022-09-16 01:05:33 +03:00
"""Скрипт для скачивания голых и не только девушек с блога blog.stanis.ru"""
2016-10-28 00:29:36 +03:00
import re
import shutil
import os.path
2016-11-13 16:05:25 +03:00
import requests
from bs4 import BeautifulSoup
2016-10-28 00:29:36 +03:00
2022-09-15 22:57:19 +03:00
__author__ = "Alexander Popov"
2022-09-16 00:46:17 +03:00
__version__ = "1.0.2"
2022-09-15 22:57:19 +03:00
__license__ = "Unlicense"
2016-10-28 00:29:36 +03:00
2022-09-16 00:46:17 +03:00
# Путь к директории для загрузки изображений
DOWNLOAD_DIRECTORY = "./images"
2016-11-13 16:05:25 +03:00
2022-09-16 00:46:17 +03:00
def get_images_links(page):
"""В качестве аргумента получает номер страницы
и возвращает списком адреса всех изображений"""
# На сайте фильтр изображений настроен через cookies
# Так что устанавливаем их в нужное положение
cookies = dict(block="951")
2017-03-07 16:11:50 +03:00
2022-09-16 00:46:17 +03:00
# Загружаем страницу и подсовываем её парсеру с необходимыми параметрами
r = requests.get(
"http://blog.stanis.ru/?back={0}".format(
page,
),
cookies=cookies,
)
2022-09-15 22:57:19 +03:00
soup = BeautifulSoup(
r.text.encode("cp1251"), "html.parser", from_encoding="windows-1251"
)
2017-03-07 16:11:50 +03:00
2022-09-16 00:46:17 +03:00
# Получаем все теги <img> на странице
img_tags = soup.findAll("img", src=re.compile("img/*"))
2017-03-07 16:11:50 +03:00
2022-09-16 00:46:17 +03:00
# Получаем все адреса изображений и сохраняем их в список img_links
img_links = list()
for image in img_tags:
img_links.append(image["src"].split("/")[1])
2017-03-07 16:11:50 +03:00
2022-09-16 00:46:17 +03:00
return img_links
2017-03-07 16:11:50 +03:00
2022-09-16 00:46:17 +03:00
def image_download(image):
"""В качестве аргумента получает уникальное имя изображения,
скачивает и сохраняет его на диск"""
2017-03-07 16:11:50 +03:00
2022-09-15 22:57:19 +03:00
response = requests.get(
"https://blog.stanis.ru/imgs/{0}".format(
image,
),
stream=True,
)
2017-03-07 16:11:50 +03:00
2022-09-16 00:46:17 +03:00
image_size = int(response.headers.get("Content-Length", 0))
2017-03-07 16:11:50 +03:00
2022-09-16 00:46:17 +03:00
if (
2022-09-16 02:07:38 +03:00
not os.path.exists("{0}/{1}".format(DOWNLOAD_DIRECTORY, image))
or int(os.path.getsize("{0}/{1}".format(DOWNLOAD_DIRECTORY, image)))
!= image_size
2022-09-16 00:46:17 +03:00
):
with open("{0}/{1}".format(DOWNLOAD_DIRECTORY, image), "wb") as out_image:
shutil.copyfileobj(
response.raw,
out_image,
)
2017-03-07 16:11:50 +03:00
2022-09-16 00:46:17 +03:00
if __name__ == "__main__":
"""Главный цикл программы"""
2022-09-16 02:01:52 +03:00
if not os.path.exists(DOWNLOAD_DIRECTORY):
os.mkdir(DOWNLOAD_DIRECTORY)
2022-09-16 00:46:17 +03:00
current_page = 0
downloaded_counter = 0
while True:
try:
# Получаем адреса изображений и сортируем их в порядке возрастания
images = get_images_links(current_page)
images.sort()
# По очереди скачиваем изображения
for image in images:
2022-09-16 02:01:52 +03:00
image_download(image)
downloaded_counter += 1
2022-09-16 00:46:17 +03:00
2022-09-16 02:07:38 +03:00
print("\rЗагружено {0} файлов.".format(downloaded_counter), end="")
2022-09-16 00:46:17 +03:00
current_page += 1
except KeyboardInterrupt:
2022-09-16 02:07:38 +03:00
print("Всего загружено {0} файлов.".format(downloaded_counter))
2022-09-16 00:46:17 +03:00
quit()