stanis-tits-scrap/stanis-tits.py

60 lines
1.9 KiB
Python
Raw Normal View History

2016-10-28 00:29:36 +03:00
#!/usr/bin/env python3
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
__author__ = 'Alexander Popov'
__version__ = '0.1.0'
2016-10-28 00:29:36 +03:00
__license__ = 'Unlicense'
2016-11-13 16:05:25 +03:00
dwnImageDir = './images'
cookies = dict(block='951')
siteUrl = 'http://blog.stanis.ru/?back=%d'
imgPage = 0
2016-11-13 16:05:25 +03:00
# create .stanis-tits.latest file and download image directory
if not os.path.exists('%s/.stanis-tits.latest' % dwnImageDir):
if not os.path.exists('%s' % dwnImageDir):
os.mkdir('%s' % dwnImageDir)
with open('%s/.stanis-tits.latest' % dwnImageDir, 'w') as f:
f.write('0')
2016-11-13 16:05:25 +03:00
with open('%s/.stanis-tits.latest' % dwnImageDir, 'r') as f:
latestDwnFile = f.read()
2016-10-28 00:29:36 +03:00
STOP = False
NEXT_LATEST = None
while STOP is False:
2016-11-13 16:05:25 +03:00
print('Loading page %d' % imgPage)
2016-10-28 00:29:36 +03:00
2016-11-13 16:05:25 +03:00
r = requests.get(siteUrl % imgPage, cookies=cookies)
2016-10-28 00:29:36 +03:00
soup = BeautifulSoup(r.text.encode('cp1251'),
"html.parser", from_encoding="windows-1251")
2016-10-28 00:29:36 +03:00
images = soup.findAll('img', src=re.compile('img/*'))
for image in images:
2016-11-13 16:05:25 +03:00
if image['src'].split('/')[1].split('.')[0] == latestDwnFile:
2016-10-28 00:29:36 +03:00
STOP = True
2016-11-13 16:05:25 +03:00
if imgPage == 0:
if NEXT_LATEST is None:
2016-10-28 00:29:36 +03:00
NEXT_LATEST = str(image['src'].split('/')[1].split('.')[0])
2016-11-13 16:05:25 +03:00
with open('%s/.stanis-tits.latest' % dwnImageDir, 'w+') as f:
2016-10-28 00:29:36 +03:00
f.write(NEXT_LATEST)
2016-11-13 16:05:25 +03:00
if not os.path.exists('%s/%s' % (dwnImageDir,
image['src'].split('/')[1],)):
2016-10-28 00:29:36 +03:00
print('\tDownload %s' % image['src'].split('/')[1])
response = requests.get('http://blog.stanis.ru/%s'
% image['src'], stream=True)
2016-11-13 16:05:25 +03:00
with open('%s/%s' % (dwnImageDir, image['src'].split('/')[1]),
'wb') as out_image:
2016-10-28 00:29:36 +03:00
shutil.copyfileobj(response.raw, out_image,)
2016-11-13 16:05:25 +03:00
imgPage += 1