-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetpic.py
196 lines (94 loc) · 3.65 KB
/
getpic.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#coding=utf-8
#!/usr/bin/python
# 导入requests库
import requests
# 导入文件操作库
import os
import bs4
from bs4 import BeautifulSoup
import sys
import importlib
importlib.reload(sys)
# 给请求指定一个请求头来模拟chrome浏览器
global headers
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'}
# 爬图地址
mziTu = 'http://www.mzitu.com/'
# 定义存储位置
global save_path
save_path = '/mnt/data/mzitu'
# 创建文件夹
def createFile(file_path):
if os.path.exists(file_path) is False:
os.makedirs(file_path)
# 切换路径至上面创建的文件夹
os.chdir(file_path)
# 下载文件
def download(page_no, file_path):
global headers
res_sub = requests.get(page_no, headers=headers)
# 解析html
soup_sub = BeautifulSoup(res_sub.text, 'html.parser')
# 获取页面的栏目地址
all_a = soup_sub.find('div',class_='postlist').find_all('a',target='_blank')
count = 0
for a in all_a:
count = count + 1
if (count % 2) == 0:
print("内页第几页:" + str(count))
# 提取href
href = a.attrs['href']
print("套图地址:" + href)
res_sub_1 = requests.get(href, headers=headers)
soup_sub_1 = BeautifulSoup(res_sub_1.text, 'html.parser')
# ------ 这里最好使用异常处理 ------
try:
# 获取套图的最大数量
pic_max = soup_sub_1.find('div',class_='pagenavi').find_all('span')[6].text
print("套图数量:" + pic_max)
for j in range(1, int(pic_max) + 1):
# print("子内页第几页:" + str(j))
# j int类型需要转字符串
href_sub = href + "/" + str(j)
print(href_sub)
res_sub_2 = requests.get(href_sub, headers=headers)
soup_sub_2 = BeautifulSoup(res_sub_2.text, "html.parser")
img = soup_sub_2.find('div', class_='main-image').find('img')
if isinstance(img, bs4.element.Tag):
# 提取src
url = img.attrs['src']
array = url.split('/')
file_name = array[len(array)-1]
# print(file_name)
# 防盗链加入Referer
headers = {'Referer': href}
img = requests.get(url, headers=headers)
# print('开始保存图片')
f = open(file_name, 'ab')
f.write(img.content)
# print(file_name, '图片保存成功!')
f.close()
except Exception as e:
print(e)
# 主方法
def main():
res = requests.get(mziTu, headers=headers)
# 使用自带的html.parser解析
soup = BeautifulSoup(res.text, 'html.parser')
# 创建文件夹
createFile(save_path)
# 获取首页总页数
img_max = soup.find('div', class_='nav-links').find_all('a')[3].text
# print("总页数:"+img_max)
for i in range(1, int(img_max) + 1):
# 获取每页的URL地址
if i == 1:
page = mziTu
else:
page = mziTu + 'page/' + str(i)
file = save_path + '/' + str(i)
createFile(file)
# 下载每页的图片
print("套图页码:" + page)
download(page, file)
if __name__ == '__main__': main()