-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveOneTieZiPhotos.py
58 lines (53 loc) · 2.02 KB
/
SaveOneTieZiPhotos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding:utf-8 -*-
__author__ = 'SUN'
'''保存一个帖子上的图片'''
from GetOnePagePhotoUrls import get_one_page_photo_urls
from GetOneTiePages import get_one_tie_pages
import requests
import random
from time import sleep
import os
class save_one_tie_zi_photos:
def __init__(self):
# 生成随机数
self.RANDOM_NUM = random.uniform(0.5, 2)
# 保存一张图片
def save_one_photo(self, photo_name, photo_url):
data = requests.get(photo_url).content
fp = open(photo_name, 'wb')
fp.write(data)
fp.close()
# 保存一个页面的图片
# def save_one_page_photos(self, url):
# X = 1
# photo_urls = get_one_page_photo_urls(url).get_photo_urls()
# print('本页共有图片', len(photo_urls), '张')
# for photo_url in photo_urls:
# if os.path.exists('%s.jpg' % X):
# pass
# else:
# print('开始保存第', X, '张')
# self.save_one_photo('%s.jpg' % X, photo_url)
# X += 1
# sleep(self.RANDOM_NUM)
# 保存一个帖子所有页面的图片
def save_all_pages_photos(self, url):
PAGE_NUM = 1
one_tie_pages = get_one_tie_pages(url).get_pages()
while PAGE_NUM <= len(one_tie_pages):
X = 1
photo_urls = get_one_page_photo_urls(one_tie_pages[PAGE_NUM - 1]).get_photo_urls()
print('本页共有图片', len(photo_urls), '张')
print(one_tie_pages[PAGE_NUM - 1])
print(len(one_tie_pages))
print(PAGE_NUM)
if os.path.exists(str(PAGE_NUM) + '页第%s张.jpg' % X):
pass
else:
for photo_url in photo_urls:
print('开始保存第', X, '张')
self.save_one_photo(str(PAGE_NUM) + '页第%s张.jpg' % X, photo_url)
X += 1
sleep(self.RANDOM_NUM)
PAGE_NUM += 1
# print('本帖子的图片已保存完成!')